[Unity].asset如何從父類數組中獲得不同子類的變量的值

 

從父類數組中 放入 不同的 子類.asset或者實體,並且 通過 父類數組 獲得 不同 子類 的變量。

方法1:

基類用virtual方法,繼承的子類用override方法獲得變量的值。(計算簡單,調用少量變量的時候 快捷)

方法2:

 FieldInfo[] fil = type.GetFields();

(遍歷每一個 子類 的變量,包括繼承 父類 的變量,對於 大量 重複 調用 的變量 效果拔羣)

 

應用:不同物品、食物:物品、裝備:物品。

建築、商店:建築、可破壞建築:建築、不可破壞建築:建築。

 


邏輯圖。

 

 


 

TestFather.cs

using UnityEngine;

//.cs腳本 繼承ScriptableObject,才能使得 CreateAssetMenu 生效
[CreateAssetMenu(fileName = "New TestFather", menuName = "TestFather")]
public class TestFather : ScriptableObject {

public int int_a = 1;
public int int_b = 2;

    public virtual TestStyle testStyle_()
    {
        return TestStyle.AA;
    }
}

 

創建一個 TestSone_1.asset,使得其變量testStyle爲 非AA,可以爲BB、CC、DD,用於區分。

TestSon_1.cs

using UnityEngine;
[CreateAssetMenu(fileName = "New TestSon", menuName = "TestSon_1")]
public class TestSon_1 : TestFather
{
    public int a_1 = 101;
    public int b_2 = 102;
    public TestStyle testStyle;

    public override TestStyle testStyle_()
    {
        return testStyle;
    }
}
public enum TestStyle
{
    AA,
    BB,
    CC,
    DD,
}

TestSon_2.cs

using UnityEngine;
[CreateAssetMenu(fileName = "New TestSon", menuName = "TestSon_2")]
public class TestSon_2 : TestFather
{
    public int a_1 = 101;
    public int b_2 = 102;
    public override TestStyle testStyle_()
    {
        return TestStyle.AA;
    }
}

 

TestArray.cs

using UnityEngine;
using System;
using System.Reflection;//FieldInfo

public class TestArray : MonoBehaviour
{
    public TestFather[] fatherArray;//這個數組裏面可以存放TestSon_1.asset和TestSon_2.asset
    public TestFather father_;//可以用TestSon_1.asset或者TestSon_2.asset來單個測試
    
    private void Start()
    {
        Debug.Log("    ");
        if (father_ != null
                    && fatherArray.Length > 0
                    && father_.GetType().ToString() == "TestSon_1")
        {
            Type type = father_.GetType();
            TestSon_1 someInstance = ScriptableObject.CreateInstance(type.ToString()) as TestSon_1;//這裏創建的someInstace 是一個 成員未賦值的 子類
            //someInstance = type;//無法轉換
            Debug.Log("   someInstance.testStyle.ToString():" + someInstance.testStyle.ToString());
            Debug.Log("    ");
            Debug.Log("   father_.testStyle_():" + father_.testStyle_()); //如果測試的 是TestSon_1.asset,則這裏會顯示 BB或者CC或者DD
            Debug.Log("    ");
            Debug.Log("   顯示子類繼承父類 的 變量的值-----------------------");
            FieldInfo[] fil = type.GetFields();//using System.Reflection;
            foreach (FieldInfo var in fil)//僅能獲取到c字段(輸出c=2)
            {
                Debug.Log("FieldInfo: " + var.Name + "=" + var.GetValue(father_));
                if (var.GetType().ToString() == "TestStyle")//找到 TestSon_1.asset中的 TestStyle類型名字爲testStyle的變量
                {
                    Debug.Log("TestStyle:"+ var.GetValue(father_));
                }
            }
        }
    }
}

 

 

 


 

 

參考資料:

1.C# Object.GetType()獲取對象的類類型/獲取類的類型

2.ScriptableObject.CreateInstance<MyScriptableObject >();
【Unity】ScriptableObject的介紹

3.

PropertyInfo、FieldInfo、MemberInfo的區別

4.

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章