在使用lua的时候需要获取物体时使用路径来查找,写一个小工具方便使用: 关键代码
GUIUtility.systemCopyBuffer = "test";这里我使用的vscode 编辑器,用了luaide插件。做ui的时候需要用路径寻找物体,这里使用一个快捷键组成一个lua代码方便使用。 模板如下
--@RefType[luaIde#"+ typeName + "] \n self.objName = self: GetCom(path, typeName);关键代码
[MenuItem("GameObject/CopyPathToUIPanelCode %Q", false, 1)] public static void CopyCodePathToUIPanel() { GameObject obj = Selection.activeGameObject; string path = Path(obj); path = path.Substring(1); List<Type> types = new List<Type>(); types.Add(typeof(UIWrapContent)); types.Add(typeof(UIGrid)); types.Add(typeof(UITable)); types.Add(typeof(UIScrollView)); types.Add(typeof(UIButton)); types.Add(typeof(UIPopupList)); types.Add(typeof(UITexture)); types.Add(typeof(UISlider)); types.Add(typeof(UICenterOnChild)); types.Add(typeof(UICenterOnClick)); types.Add(typeof(UILabel)); types.Add(typeof(UISprite)); string codeStr = "self: GetCom(\"{0}\", {1});"; string typeStr = ""; string prefix = ""; foreach (var item in types) { Component test = obj.GetComponent(item); if(test != null) { typeStr = item.Name; prefix = "--@RefType[luaIde#"+ item.Name + "] \n "; break; } } if(typeStr == "") codeStr = "self:GetCom(\"{0}\");"; string codestr = string.Format(codeStr, path, "NGUIComponent."+typeStr); codestr = prefix + "self." + obj.name + " = " + codestr; Debug.Log(codestr); GUIUtility.systemCopyBuffer = codestr; }lua代码的定义,这里的定义时为了统一维护
NGUIComponent = { UIPanel = "UIPanel", UIWidget = "UIWidget", UISprite = "UISprite", UIButton = "UIButton", UIPopupList = "UIPopupList", UILabel = "UILabel", UIGrid = "UIGrid", UIToggle = "UIToggle", UIWrapContent = "UIWrapContent", UIScrollView = "UIScrollView", UIScrollBar = "UIScrollBar", UITexture = "UITexture", UITable = "UITable", UISlider = 'UISlider', UICenterOnChild = "UICenterOnChild", UICenterOnClick = "UICenterOnClick", UIProgressBar = "UIProgressBar", UIEventListener = "UIEventListener", UIEventTrigger = "UIEventTrigger", UIInput = "UIInput", TweenAlpha = "TweenAlpha", }