日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity插件扩展中组件常用的几个方法

發布時間:2025/3/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity插件扩展中组件常用的几个方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近為美術編寫一個Unity編輯器的擴展,主要為了減輕美術在修改預制對象時的機械化操作的繁瑣和出錯。具體實現的幾個功能:

1、刪除指定組件;

2、復制、粘貼指定的組件;

3、重新關聯新的屬性;

4、重新保存預制對象;

?

一、刪除指定類型的組件

public static void RemoveComponentHandler(GameObject gameObject, Type componentType){foreach (var component in gameObject.GetComponents<Component>()){if (component.GetType() == componentType){GameObject.DestroyImmediate(component);}} }

?

二、復制組件(這里實現的是一次僅復制一個某類型的組件)

public static void CopyComponentHandler(Type componentType, GameObject fromGameObject, GameObject toGameObject){RemoveComponentHandler(toGameObject, componentType);// 查找需要復制的 ComponentComponent needCopyComponent = null;foreach (var component in fromGameObject.GetComponents<Component>()){if (component.GetType() == componentType){needCopyComponent = component;break;}}// 進行粘貼操作// http://answers.unity3d.com/questions/907294/copy-all-components-from-a-gameobject-and-paste-to.html UnityEditorInternal.ComponentUtility.CopyComponent(needCopyComponent);UnityEditorInternal.ComponentUtility.PasteComponentAsNew(toGameObject);}

?

三、關聯新屬性

就是遍歷指定的GameObject,然后找到它附加的組件,重新設置其值即可。

?

四、替換預制對象

GameObject activeGameObject = Selection.activeGameObject; if (activeGameObject != null) {// 獲取當前的idif (new Regex(@"^\d+h$").IsMatch(activeGameObject.name)){UnityEngine.Object parentObject = null;string strPrefabPath = "";if (PrefabUtility.GetPrefabType(activeGameObject) == PrefabType.PrefabInstance){parentObject = EditorUtility.GetPrefabParent(activeGameObject);strPrefabPath = AssetDatabase.GetAssetPath(parentObject);}// 查找idstring strId = new Regex(@"h$").Replace(activeGameObject.name, "");// 第六步 保存預制對象string strCurrSelectPrefabName = activeGameObject.name;if (strPrefabPath.EndsWith(".prefab")){// string[] dependPaths = AssetDatabase.GetDependencies(strPrefabPath);GameObject go = GameObject.Instantiate(GameObject.Find(strCurrSelectPrefabName)) as GameObject;PrefabUtility.ReplacePrefab(go, parentObject, ReplacePrefabOptions.ConnectToPrefab);GameObject.DestroyImmediate(activeGameObject);go.name = strCurrSelectPrefabName; AssetDatabase.Refresh();}Debug.Log("預制對象 " + strCurrSelectPrefabName + " 修改完成。");}else{Debug.Log("當前選中的GameObject命名不符合要求,格式:id+h。\tGameObject Name : " + activeGameObject.name);} }

最核心的幾行代碼:

1、實例化一個新的GameObject;

2、替換預制對象;

3、銷毀老的GameObject;

4、刷新資源;

?

?

對于美術的同事來講,最復雜、麻煩的莫過于重新關聯屬性,特別是骨骼動畫。因為之前沒有統一的規范,所以關聯哪一段動畫實際上是需要一層一層找的,我看著他們找都覺得累,怎么辦呢?我想到一個辦法,就是通過name查找新的組件,然后重新賦值關聯。通過Name查找某個GameObject下的子節點(前提條件是該Name唯一)

public static GameObject FindChildGameObject(GameObject parent, string childName){if (parent.name == childName){return parent;}if (parent.transform.childCount < 1){return null;}GameObject obj = null;for (int i = 0; i < parent.transform.childCount; i++){GameObject go = parent.transform.GetChild(i).gameObject;obj = FindChildGameObject(go, childName);if (obj != null){break;}}return obj;}

?

上面基本上實現了,組件幾個常用的方法:

1、添加組件(先復制后粘貼);

2、刪除組件;

3、通過名字查找子組件;

4、更新預制對象;

總結

以上是生活随笔為你收集整理的Unity插件扩展中组件常用的几个方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。