您的位置:首页 > 编程语言 > Lua

项目后期Lua接入笔记07--预制属性转lua代码

2017-03-31 20:39 507 查看
有些功能可能已经完成一部分了,或者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代码直接粘贴就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua 项目 后期 接入 unity