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); }
private void ToggleLoadPrefab() { if (instanceIDProp.intValue == 0 || EditorUtility.InstanceIDToObject(instanceIDProp.intValue) == null) { ActionEditorUtility.LoadPrefab(prefabProp, instanceIDProp); } else { ActionEditorUtility.SavePrefab(instanceIDProp); } }
private void CloseAllCreated(SerializedProperty arrayProp) { for (int i = 0; i < arrayProp.arraySize; i++) { var itemProp = arrayProp.GetArrayElementAtIndex(i); var instanceIDPorp = itemProp.FindPropertyRelative("instanceID"); var obj = EditorUtility.InstanceIDToObject(instanceIDPorp.intValue); if (obj != null) { var matrixProp = itemProp.FindPropertyRelative("matrix"); var rematrixProp = itemProp.FindPropertyRelative("rematrix"); ActionEditorUtility.SavePrefab(instanceIDPorp, rematrixProp, matrixProp); DestroyImmediate(obj); } itemProp.isExpanded = false; } }
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); } } } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { FindCommonPropertys(property); prefabProp = property.FindPropertyRelative("prefab"); if (prefabProp.objectReferenceValue != null) { label = new GUIContent(prefabProp.objectReferenceValue.name); } var rect = new Rect(position.x, position.y, position.width * 0.9f, EditorGUIUtility.singleLineHeight); var str = prefabProp.objectReferenceValue == null ? "" : prefabProp.objectReferenceValue.name; GUI.contentColor = Color.cyan; if (GUI.Button(rect, str, EditorStyles.toolbarDropDown)) { property.isExpanded = !property.isExpanded; if (property.isExpanded) { ActionEditorUtility.LoadPrefab(prefabProp, containsCommandProp, containsPickupProp, instanceIDProp, rematrixProp, matrixProp); } else { ActionEditorUtility.SavePrefab(instanceIDProp, rematrixProp, matrixProp); } } GUI.contentColor = Color.white; InformationShow(rect); rect = new Rect(position.max.x - position.width * 0.1f, position.y, 20, EditorGUIUtility.singleLineHeight); switch (Event.current.type) { case EventType.DragUpdated: if (rect.Contains(Event.current.mousePosition)) { DragAndDrop.visualMode = DragAndDropVisualMode.Move; } break; case EventType.DragPerform: if (rect.Contains(Event.current.mousePosition)) { Debug.Log(DragAndDrop.objectReferences.Length); if (DragAndDrop.objectReferences.Length > 0) { var obj = DragAndDrop.objectReferences[0]; if (obj is GameObject) { ActionEditorUtility.InsertItem(prefabProp, obj); } DragAndDrop.AcceptDrag(); } Event.current.Use(); } break; case EventType.DragExited: break; } if (prefabProp.objectReferenceValue != null) { if (GUI.Button(rect, "", EditorStyles.objectFieldMiniThumb)) { EditorGUIUtility.PingObject(prefabProp.objectReferenceValue); } } else { prefabProp.objectReferenceValue = EditorGUI.ObjectField(rect, null, typeof(GameObject), false); } rect = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight); if (property.isExpanded) { DrawOptions(rect); } }