public static void ResetAllCollider() { GameColliderSet colliderSet = new GameColliderSet(); var guids = AssetDatabase.FindAssets("t:GameObject", new string[] { "Assets/ResourceEx/Prefab" }); int count = guids.Length; for (int i = 0; i < count; i++) { EditorUtility.DisplayProgressBar("更新碰撞框数据", "正在更新碰撞框数据(" + i + "/" + count + ")", i / (float)count); string goPath = AssetDatabase.GUIDToAssetPath(guids[i]); GameObject gameObject = AssetDatabase.LoadAssetAtPath <GameObject>(goPath); GameColliderView colliderView = gameObject.GetComponent <GameColliderView>(); if (colliderView != null) { GameColliderItem colliderItem = new GameColliderItem(); colliderItem.path = goPath; FindCollider(gameObject.transform, gameObject.transform, colliderItem); colliderSet.mLstColliderItem.Add(colliderItem); } } NEUtil.SerializerObject(GameColliderCfgSys.GameColliderPath, colliderSet, GameColliderCfgSys.RelateTypes); EditorUtility.ClearProgressBar(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
private void LoadData() { TextAsset textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(GameColliderCfgSys.GameColliderPath); m_cColliderSet = null; if (textAsset != null) { var bytes = textAsset.bytes; m_cColliderSet = NEUtil.DeSerializerObjectFromBuff(bytes, typeof(GameColliderSet), GameColliderCfgSys.RelateTypes) as GameColliderSet; } if (m_cColliderSet == null) { m_cColliderSet = new GameColliderSet(); } GameColliderView colliderView = (GameColliderView)target; PrefabType prefabType = PrefabUtility.GetPrefabType(colliderView.gameObject); Debug.Log(prefabType); m_bIsPrefab = (prefabType == PrefabType.Prefab || prefabType == PrefabType.PrefabInstance); if (m_bIsPrefab) { UnityEngine.Object prefab = null; if (prefabType == PrefabType.PrefabInstance) { prefab = PrefabUtility.GetPrefabParent(colliderView.gameObject); } else { prefab = colliderView.gameObject; } Debug.Log(prefab); if (m_cColliderSet != null && prefab != null) { string path = AssetDatabase.GetAssetPath(prefab); Debug.Log("path:" + path); m_cColliderItem = m_cColliderSet.GetColliderItem(path); if (m_cColliderItem == null) { m_cColliderItem = new GameColliderItem(); m_cColliderItem.path = path; m_cColliderSet.mLstColliderItem.Add(m_cColliderItem); } } } }
public override void OnInspectorGUI() { GameColliderView colliderView = (GameColliderView)target; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("GameCollider:"); EditorGUILayout.EndHorizontal(); PrefabType prefabType = PrefabUtility.GetPrefabType(colliderView.gameObject); bool isPrefab = prefabType == PrefabType.Prefab || prefabType == PrefabType.PrefabInstance; if (isPrefab != m_bIsPrefab) { LoadData(); } m_bIsPrefab = isPrefab; if (m_cColliderItem == null) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("当前对象必须是一个预制体"); EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("path:" + m_cColliderItem.path); EditorGUILayout.EndHorizontal(); int dataCount = m_cColliderItem.mLstData.Count; for (int i = 0; i < dataCount; i++) { BaseGameColliderData colliderData = m_cColliderItem.mLstData[i]; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(" " + colliderData.name); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("\tcolliderType", colliderData.gameColliderType.ToString()); EditorGUILayout.EndHorizontal(); switch (colliderData.gameColliderType) { case GameColliderType.CircleCollider: OnCircleGUI((CircleColliderData)colliderData); break; case GameColliderType.RectCollider: OnRectGUI((RectColliderData)colliderData); break; default: EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("\tcolliderType=[" + colliderData.gameColliderType.ToString() + "]需要编写编辑器代码"); EditorGUILayout.EndHorizontal(); break; } } } if (GUILayout.Button("查找所有碰撞框")) { if (m_cColliderItem != null) { m_cColliderItem.mLstData.Clear(); } Transform trans = colliderView.transform; FindCollider(trans, trans, m_cColliderItem); if (GUI.changed) { EditorUtility.SetDirty(colliderView.gameObject); } serializedObject.ApplyModifiedProperties(); SaveData(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }