【问题标题】:Dynamic if Condition from any class In UnityUnity中任何类的动态if条件
【发布时间】:2017-12-12 02:42:12
【问题描述】:

我想创建一个动态条件语句。

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;
    }
}

我想获取 Unity Editor 或 C# 库关键字。

目的是创造类似上面的东西。

  1. 游戏中的互动元素可能会发生变化。
  2. 可以扩展或添加游戏内元素。
  3. 我不想修改每个更改或扩展的条件语句。

例子

ex1 ex2 我记得在 C# 中或在执行期间添加类之后看到了用于动态编译的库。

我当然知道动态不是通用的。 我想做一些类似星际争霸银河编辑器的验证。

这是一个游戏示例

[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.

【问题讨论】:

    标签: c# unity3d dynamic conditional-statements


    【解决方案1】:

    由于被测试的类型会有所不同,因此将其设为通用可能会有所帮助。这样你的 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);
    

    【讨论】:

    • 我能得到这些信息吗? >>> 'SomeClass.GetPropertiesList' arrayList 或任何关于成员的信息列表。
    • 您是在询问有关SomeClass 的详细信息吗?这只是我为示例编写的一个类。或者你在问别的问题 - 我不明白。
    • 例如:类单元有[int] Health,[int] Atk,[bool] isAttack。在代码“Unit.GetGetPropertiesList()”中。然后我可以在 Array 中获取 informationsCollection。
    • 抱歉,我仍然没有关注 - 这是这个问题的一部分还是关于如何列出类型属性的不同问题?
    • 非常感谢您的回答。当问题来来去去时,我得到了没有任何问题的关键字。
    猜你喜欢
    • 2021-11-18
    • 2019-01-11
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2021-12-26
    • 2021-09-09
    相关资源
    最近更新 更多