private void SaveDataToAsset(bool isNew) { ModularArmor newArmor = ScriptableObject.CreateInstance <ModularArmor>(); newArmor.armorType = armorTypes[armorTypeIndex]; foreach (var armorColor in newArmor.armorColors) { armorColor.color = previewMaterial.GetColor(armorColor.property); } newArmor.armorParts = armorParts.ToArray(); for (int i = 0; i < newArmor.armorParts.Length; i++) { newArmor.armorParts[i].partID = activePartID[i]; } if (isNew) { string newAssetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath + "/" + armorName + ".asset"); AssetDatabase.CreateAsset(newArmor, newAssetPath); } else { if (existingArmor == null) { Debug.LogWarning("Nothing to overwrite"); return; } if (armorName != existingArmor.name) { string existingPath = AssetDatabase.GetAssetPath(existingArmor); AssetDatabase.RenameAsset(existingPath, armorName); } EditorUtility.CopySerializedIfDifferent(newArmor, existingArmor); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
private void ResetAll() { DestroyImmediate(previewEditor); existingArmor = null; Initialize(); }
private void OnGUI() { GUILayout.BeginArea(new Rect(previewRect.x + windowPadding.x, previewRect.y + windowPadding.y, previewRect.width - windowPadding.x, previewRect.height - windowPadding.y)); if (previewPrefab != null) { if (previewEditor == null) { previewEditor = Editor.CreateEditor(previewPrefab, typeof(CustomPreviewEditor)) as CustomPreviewEditor; previewEditor.OnPreviewObjectInstantiated += LoadPreviewObject; previewEditor.TargetAsset = previewPrefab; } else { if (characterManager == null) { LoadPreviewObject(previewEditor.TargetObject); } } if (previewEditor.HasPreviewGUI()) { previewEditor.OnInteractivePreviewGUI(new Rect(0, 0, previewRect.width - windowPadding.x, previewRect.height - windowPadding.y), null); } } GUILayout.EndArea(); GUILayout.BeginArea(new Rect(bodyOptionsRect.x + windowPadding.x, bodyOptionsRect.y + windowPadding.y, bodyOptionsRect.width - windowPadding.x, bodyOptionsRect.height - windowPadding.y)); GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); if (GUILayout.Button("Male")) { characterManager.SwapGender(Gender.Male); currentGender = Gender.Male; } if (GUILayout.Button("Female")) { characterManager.SwapGender(Gender.Female); currentGender = Gender.Female; } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button("ShowBody")) { characterManager.ToggleBaseBodyDisplay(true); } if (GUILayout.Button("HideBody")) { characterManager.ToggleBaseBodyDisplay(false); } GUILayout.EndHorizontal(); if (GUILayout.Button("Reset")) { ResetAll(); } GUILayout.Label("Existing Modular Armor Asset"); EditorGUI.BeginChangeCheck(); existingArmor = EditorGUILayout.ObjectField(existingArmor, typeof(ModularArmor), false) as ModularArmor; if (EditorGUI.EndChangeCheck()) { if (existingArmor != null) { armorName = existingArmor.name; ResetParts(); SetupPartsFromExisting(); SetMaterialColorsToExisting(); characterManager.SetAllPartsMaterial(previewMaterial); InitializeColors(); } else { ResetAll(); } } GUILayout.EndVertical(); GUILayout.EndArea(); GUILayout.BeginArea(new Rect(armorOptionsRect.x + windowPadding.x, armorOptionsRect.y + windowPadding.y, armorOptionsRect.width - windowPadding.x, armorOptionsRect.height - windowPadding.y)); EditorGUI.BeginChangeCheck(); armorTypeIndex = EditorGUILayout.Popup(armorTypeIndex, Array.ConvertAll <ModularArmorType, string>(armorTypes, x => x.ToString()), GUILayout.Width(340)); if (EditorGUI.EndChangeCheck()) { ResetParts(); armorParts = GetArmorParts(armorTypes[armorTypeIndex]); SetupParts(); } GUILayout.BeginVertical(); armorScrollView = GUILayout.BeginScrollView(armorScrollView); if (armorParts.Count > 0) { for (int i = 0; i < armorParts.Count; i++) { GUILayout.BeginHorizontal(); GUILayout.Label(armorParts[i].bodyType.ToString()); EditorGUI.BeginChangeCheck(); int maxPartID = -1; if (characterManager != null) { maxPartID = characterManager.GetCharacterBody()[armorParts[i].bodyType].Length - 1; } activePartID[i] = EditorGUILayout.IntSlider(activePartID[i], -1, maxPartID); if (EditorGUI.EndChangeCheck()) { if (activePartID[i] > -1) { characterManager.ActivatePart(armorParts[i].bodyType, activePartID[i]); } else { characterManager.DeactivatePart(armorParts[i].bodyType); } armorParts[i].partID = activePartID[i]; } GUILayout.EndHorizontal(); } } GUILayout.EndScrollView(); GUILayout.EndArea(); GUILayout.BeginArea(new Rect(colorOptionsRect.x + windowPadding.x, colorOptionsRect.y + windowPadding.y, colorOptionsRect.width - windowPadding.x, colorOptionsRect.height - windowPadding.y)); foreach (var armorColor in armorColors) { SetupColorFields(ref armorColor.color, armorColor.property, armorColor.property); } GUILayout.EndArea(); GUILayout.EndVertical(); GUILayout.BeginArea(new Rect(armorNameRect.x + windowPadding.x, armorNameRect.y + windowPadding.y, armorNameRect.width - windowPadding.x, armorNameRect.height - windowPadding.y)); GUILayout.BeginHorizontal(); GUILayout.Label("Armor Name: ", GUILayout.Width(75)); armorName = GUILayout.TextField(armorName, GUILayout.Width(260)); GUILayout.EndHorizontal(); GUILayout.EndArea(); GUILayout.BeginArea(new Rect(saveButtonRect.x + windowPadding.x, saveButtonRect.y + windowPadding.y, saveButtonRect.width - windowPadding.x, saveButtonRect.height - windowPadding.y)); GUILayout.BeginHorizontal(); if (GUILayout.Button("Save as New")) { SaveDataToAsset(true); } if (GUILayout.Button("Overwrite Existing")) { SaveDataToAsset(false); } GUILayout.EndHorizontal(); if (GUILayout.Button("Create Icon")) { iconWindow = EditorWindow.GetWindow <IconCreatorWindow>(); iconWindow.Show(); if (iconWindow.PreviewEditor != null) { DestroyImmediate(iconWindow.PreviewEditor); } iconWindow.TargetObject = previewPrefab; iconWindow.OnPreviewCreated += SetupIconPreview; } GUILayout.EndArea(); }