有些功能可能已經完成一部分了,或者lua中自己寫find屬性很麻煩,字符串很長,這裏我們需要自己寫一個工具來獲取這些數據。
需要的功能就是預製體已經有c#並將屬性拖入代碼文件了,我們將對應的路徑轉成lua代碼
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
public class PrefabLua
{
[MenuItem("GameObject/GameTools/GetPrefabVar")]
private static void GetPrefabVar()
{
if (Selection.gameObjects.Length != 1)
{
Debug.LogError("只能選擇一個");
return;
}
MonoBehaviour mono = Selection.gameObjects[0].GetComponent<MonoBehaviour>();
if (mono == null)
{
Debug.LogError("請確保預製體上有腳本");
return;
}
//Debug.Log(mono.GetType());
Type t = mono.GetType();
PropertyInfo property = t.GetProperty("transform");
Transform trans = property.GetValue(mono, null) as Transform;
//取得序列化名字列表
List<string> serializeNames = new List<string>();
SerializedObject serializedObject = new SerializedObject(mono);
SerializedProperty it = serializedObject.GetIterator();
while (it.Next(true))
{
serializeNames.Add(it.name);
}
FieldInfo[] fis = t.GetFields();
StringBuilder sb = new StringBuilder();
foreach (FieldInfo fi in fis)
{
//Debug.Log(fi.Name);
if (!serializeNames.Contains(fi.Name)) continue;
object obj = fi.GetValue(mono);
Type objType = obj.GetType();
if (objType == typeof(int) || objType == typeof(bool))//常見類型
{
sb.AppendLine(fi.Name + " = " + obj + ";");
}
else if (objType == typeof(float))//浮點數
{
sb.AppendLine(fi.Name + " = " + obj + "f;");
}
else if (objType == typeof(Vector3))//向量
{
string[] ss = obj.ToString().Replace("(","").Replace(")","").Split(',');
sb.AppendLine(fi.Name + " = new Vector3(" + ss[0] + "f," + ss[1] + "f," + ss[2] + "f);");
}
else if (obj.GetType().BaseType == typeof(System.Enum))//枚舉
{
sb.AppendLine(fi.Name + " = " + objType.ToString().Replace("+", ".") + "." + obj + ";");
}
else if (obj.GetType().BaseType == typeof(System.Array))//數組
{
Array gg = obj as Array;
string arrayType = obj.GetType().Name.Replace("[]", "");
for (int i = 0; i < gg.Length; i++)
{
Component cp = gg.GetValue(i) as Component;
Transform fieldTrans = null;
if (cp == null)
{
GameObject go = gg.GetValue(i) as GameObject;
fieldTrans = go.transform;
}
else
{
fieldTrans = cp.transform;
}
sb.AppendLine(string.Format("{0}[{1}] = transform:Find(\"{2}\"):GetComponent('{3}');", fi.Name, i, GameUtils.GetFullName(fieldTrans, trans), arrayType));
}
}
else if (obj.ToString().StartsWith("System.Collections.Generic.List"))//鏈表
{
string listType = objType.ToString().Split('[')[1].Split(']')[0];
IList ff = obj as IList;
for (int i = 0; i < ff.Count; i++)
{
Component cp = ff[i] as Component;
Transform fieldTrans = null;
if (cp == null)
{
GameObject go = ff[i] as GameObject;
fieldTrans = go.transform;
}
else
{
fieldTrans = cp.transform;
}
sb.AppendLine(string.Format("{0}[{1}] = transform:Find(\"{2}\"):GetComponent('{3}');", fi.Name, i, GameUtils.GetFullName(fieldTrans, trans), listType));
}
}
else
{
Component cp = obj as Component;
PropertyInfo fieldProperty = fi.FieldType.GetProperty("transform");
Transform fieldTrans = fieldProperty.GetValue(obj, null) as Transform;
sb.AppendLine(string.Format("{0} = transform:Find(\"{1}\"):GetComponent('{2}');", fi.Name, GameUtils.GetFullName(fieldTrans, trans), fi.FieldType));
}
}
string str = sb.ToString();
str = str.Replace(":GetComponent('UnityEngine.Transform')", "");
str = str.Replace(":GetComponent('UnityEngine.GameObject')", ".gameObject");
str = str.Replace("True", "true").Replace("False", "false");
str = str.Replace(trans.name + "/", "");
EditorTools.CopyText(str);
}
}
對應的copyText的代碼
/// <summary>
/// 將字符串複製到剪切板
/// </summary>
/// <param name="text"></param>
public static void CopyText(string text)
{
if (string.IsNullOrEmpty(text)) return;
TextEditor te = new TextEditor();
te.text = text;
te.OnFocus();
te.Copy();
}
使用時選擇掛了腳本的gameobject,右鍵選擇GameTools/GetPrefabVar,然後在lua代碼直接粘貼就行了。