private void DrawPrefabElement(Rect rect, int index, bool isActive, bool isFocused) { var btnRect = new Rect(rect.x, rect.y, rect.width - 60, EditorGUIUtility.singleLineHeight); var titleRect = new Rect(rect.x + btnRect.width * 0.5f, rect.y, btnRect.width * 0.5f, btnRect.height); var obj = prefabsProp.GetArrayElementAtIndex(index); var instenceID = GetInstenceID(obj.objectReferenceValue); var objName = obj.objectReferenceValue == null ? "" : obj.objectReferenceValue.name; if (GUI.Button(btnRect, objName, EditorStyles.toolbarDropDown)) { if (obj.objectReferenceValue != null) { if (instenceID == 0 || EditorUtility.InstanceIDToObject(instenceID) == null) { ActionEditorUtility.LoadPrefab(obj.objectReferenceValue as GameObject, ref instenceID); } else { ActionEditorUtility.SavePrefab(ref instenceID); } } SaveInstenceID(obj.objectReferenceValue, instenceID); } if (instenceID != 0) { EditorGUI.LabelField(titleRect, instenceID.ToString()); } var objRect = new Rect(rect.x + btnRect.width, rect.y, 60, btnRect.height); obj.objectReferenceValue = EditorGUI.ObjectField(objRect, obj.objectReferenceValue, typeof(GameObject), false); }
internal static GameObject LoadPrefab(string guid, SerializedProperty instenceIDProp, SerializedProperty coordinateProp) { if (instenceIDProp.intValue != 0) { var gitem = EditorUtility.InstanceIDToObject(instenceIDProp.intValue); if (gitem != null) { GameObject.DestroyImmediate(gitem); } } if (string.IsNullOrEmpty(guid) || string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(guid))) { return(null); } else { GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(AssetDatabase.GUIDToAssetPath(guid)); var id = instenceIDProp.intValue; var instenceObj = ActionEditorUtility.LoadPrefab(prefab, ref id); instenceIDProp.intValue = id; ActionEditorUtility.LoadCoordinatePropInfo(coordinateProp, instenceObj.transform); return(instenceObj); } }
private void DrawPrefabsHeader(Rect rect) { var titleRect = new Rect(rect.x, rect.y, 100, EditorGUIUtility.singleLineHeight); EditorGUI.LabelField(titleRect, "预制体列表"); var createAllRect = new Rect(rect.x + rect.width - 3 * btnWidth, rect.y, btnWidth, EditorGUIUtility.singleLineHeight); if (GUI.Button(createAllRect, "clear")) { var confer = EditorUtility.DisplayDialog("温馨提示", "如需清除所有记录的预制体,请按确认", "确认"); if (confer) { prefabsProp.ClearArray(); } } createAllRect.x += btnWidth; if (GUI.Button(createAllRect, "open")) { for (int i = 0; i < prefabsProp.arraySize; i++) { var obj = prefabsProp.GetArrayElementAtIndex(i); if (obj.objectReferenceValue != null) { var instenceID = GetInstenceID(obj.objectReferenceValue); ActionEditorUtility.LoadPrefab(obj.objectReferenceValue as GameObject, ref instenceID); SaveInstenceID(obj.objectReferenceValue, instenceID); } } } createAllRect.x += btnWidth; if (GUI.Button(createAllRect, "save")) { for (int i = 0; i < prefabsProp.arraySize; i++) { var obj = prefabsProp.GetArrayElementAtIndex(i); if (obj.objectReferenceValue != null) { var instenceID = GetInstenceID(obj.objectReferenceValue); ActionEditorUtility.SavePrefab(ref instenceID); SaveInstenceID(obj.objectReferenceValue, instenceID); } } } }