c#Unity:从动态类型

时间:2018-06-12 15:25:41

标签: c# dynamic types properties

我试图将多个Scriptable对象中的数据传递给一个类型的Struct。可编写脚本的对象具有相同的结构和相同的名称变量,但它们的类型名称不同(来自谷歌表的数据)。我希望有一个脚本为所有这种脚本化对象执行此操作。我不想为每个人制作新的类转换器(会有很多这些对象)

 public class JeffData : MonoBehaviour {

 public Jeff_Story01 JeffStory01; // scriptableobject
 public Jeff_Story02 JeffStory02; // scriptableobject
 public Jeff_Story03 JeffStory03; // scriptableobject

}

公共类RawStoryDataToStruct {

 private List<StoryData> jeffStories = new List<StoryData> ();
 private StoryData data;

 public RawStoryDataToStruct(Jeff_Story01 rawData) // I want this to take each scriptableobject
 {
     for (int i = 0; i < rawData.dataArray.Length; i++) { // and still have access to variables
         StoryData data = new StoryData (
             rawData.dataArray[i].No,
             rawData.dataArray[i].Sceneid,
             rawData.dataArray[i].When,
             rawData.dataArray[i].Chancepercent,
             rawData.dataArray[i].Condition1,
             rawData.dataArray[i].Condition1val,
             rawData.dataArray[i].Condition2,
             rawData.dataArray[i].Condition2val,
             rawData.dataArray[i].Condition3,
             rawData.dataArray[i].Condition3val
         );

         jeffStories.Add (data);
     }
 }

}

dataArray是Jeff_Story01Data的数组,它包含我想要的属性。字符串,整数,浮点数(可能是数组) 对于每个可编写脚本的对象都是他们自己的类,即:Jeff_Story01 - Jeff_Story01Data,Jeff_Story02 - Jeff_Story02Data等。

我试过这种方式但仍然失败了。

 public class RawStoryDataToStruct {

 private List<StorySceneData> scences = new List<StorySceneData> ();

 public RawStoryDataToStruct(ScriptableObject rawData)
 {
     if (rawData != null) {

         Type T = rawData.GetType();
         MemberInfo[] myMemberInfo = T.GetMember ("dataArray");
         Type TT = (Type)((FieldInfo)myMemberInfo[0]).GetValue(rawData).GetType(); // works only for 0 index..
         Type TTelement = TT.GetElementType ();
         Debug.Log (TTelement.GetProperty ("Sceneid").PropertyType.Name); // string
         // I would like to use GetValue(obj)); but im always getting object does not match target type c# getvalue

     }
 }

}

object TTobject = TTelement as object;

Debug.Log(TTelement.GetProperty(&#34; Sceneid&#34;)。GetValue(TTobject,null)); //错误

我不知道如何在这里使用GetValue。以及如何获取dataArray的所有索引.. 我不想为每个SO上课,但我没有太多时间做这件事。请帮忙

0 个答案:

没有答案