2017-07-07 4 views
-1

動的条件文を作成したいとします。Dynamic if Unityの任意のクラスの条件

class Condition 
{ 
    targetObject; //Class or Struct or anythings.. 
    targetMemberVariable; //the Member in targetObject 

    conditionFilter; 
    //It depends on the type of targetMemberVariable. 
    //targetMemberVariable is Float type, 
    //then conditionFilter automatically becomes a Float type. 
    //No need to change it in "Inspector" 

    comparisonValue; 
    //This is based on the type of conditionFilter. 
    //If it is a float type, the "Inspector" takes a float as its value. 

    Public bool CheckCondition(object targetObject) 
    { 
     //It checks with the above information and returns a true value. 
     return true or false; 
    } 
} 

ユニティエディタまたはC#ライブラリのキーワードを取得します。

意図は、上記のようなものを作成することです。

  1. ゲーム内の対話要素が変更される可能性があります。
  2. ゲーム内の要素を展開または追加できます。
  3. 私は変更または拡張ごとに条件文を変更したくありません。

ex1 ex2 私はC#での動的コンパイル中または実行時にクラスを追加した後に使用するライブラリを見て覚えています。

もちろん、私は動的が普遍的ではないことを知っています。 私はStarCraft Galaxy Editorの検証のようなことをしたい。これは、一般的な作り。ここ

がゲームの一例である

[Bleeding shot] 
Give 10 damage to enemies. 
If the enemy's Health is below 20, it adds 10 damage. 

unit,health,amount,<=(below) 
true-> adds 10 damage. 
false -> none. 


[Hooking] 
If the enemy is enemy, it gives 5 damage and pulls to the front. 
If the target is ally, pull to the front and reduce cooldown to 5 seconds. 

unit,Tag,Enemy(enum),== 
true-> 5 damage and pulls to the front. 
false-> pull to the front and reduce cooldown to 5 seconds. 

[Healing the Moon] 
Heal the target by 10. 
If the current time is night, heal 10 more. 

GameTimer,DayNight(Enum),Night,== 
true->heal 10 more. 

答えて

2

テストされているタイプが異なるので、おそらく役立つだろう。あなたのCheckConditionメソッドは型安全です。

インターフェイスから開始することもできます。道を進むと、特定のメンバーとある程度の閾値を比較するよりも複雑な条件が必要になる場合があります。

public interface ICondition<T> 
{ 
    bool CheckCondition(T subject); 
} 

は、私はほとんどの場合、あなたはそれが簡単にちょうどクラスの店舗会員情報利用の反射に比べて、明示的に状態をチェックするために見つけることを疑います。例えば

、それは読むことはとても簡単です

public class SomeClass 
{ 
    public int Value { get; set; } 
} 

public class MinimumValueCondition : ICondition<SomeClass> 
{ 
    public bool CheckCondition(SomeClass subject) 
    { 
     return subject.Value > 5; 
    } 
} 

は、何の反射は必要ありません。条件を評価するために必要ないくつかのデータが可変である場合、あなたはそれを渡すことができます。

public class MinimumValueCondition : ICondition<SomeClass> 
{ 
    private readonly int _minimumValue; 

    public MinimumValueCondition(int minimumValue) 
    { 
     _minimumValue = minimumValue; 
    } 

    public bool CheckCondition(SomeClass subject) 
    { 
     return subject.Value >= _minimumValue; 
    } 
} 

また、引数として関数を受け取り条件を書くことができます。

public class Condition<T> : ICondition<T> 
{ 
    private readonly Func<T, bool> _conditionFunction; 

    public Condition(Func<T, bool> conditionFunction) 
    { 
     _conditionFunction = conditionFunction; 
    } 

    public bool CheckCondition(T subject) 
    { 
     return _conditionFunction(subject); 
    } 
} 

こうして、式または方法からConditionを作成することができます。

var notZeroCondition = new Condition<SomeClass>(subject => subject.Value != 0); 
var someOtherCondition = new Condition<SomeClass>(SomeEvaluationMethod); 
+0

私はこの情報を入手できますか? >>> 'SomeClass.GetPropertiesList' arrayListまたはメンバーに関する情報のanyList。 – angdroid

+0

'SomeClass'についての詳細は?私はこの例のために作ったクラスです。それとも他のことを求めているのですか?私は理解しません。 –

+0

例:クラスユニットには[int] Health、[int] Atk、[bool] isAttackがあります。 Code 'Unit.GetGetPropertiesList()'にあります。私はArrayでinformationsCollectionを取得できます。 – angdroid