public void PrepareToImport(bl_PlayerSync player, bl_WeaponExported exportedWeapon) { Player = player; WeaponToImport = exportedWeapon; isExport = false; minSize = new Vector2(400, 200); maxSize = new Vector2(600, 350); }
public override void OnInspectorGUI() { bl_WeaponExported script = (bl_WeaponExported)target; if (script.WeaponInfo != null) { GUILayout.BeginVertical(GUILayout.Height(200)); GUILayout.FlexibleSpace(); GUILayout.Label(script.WeaponInfo.Name.ToUpper(), EditorStyles.toolbarButton); GUILayout.Space(5); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); if (script.WeaponInfo.GunIcon != null) { float aspect = 256f / (float)script.WeaponInfo.GunIcon.texture.width; GUILayout.Label(script.WeaponInfo.GunIcon.texture, GUILayout.Width(256), GUILayout.Height(script.WeaponInfo.GunIcon.texture.height * aspect)); } GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.Label("FPWeapon: "); Texture icon = script.FPWeapon == null ? badIcon : goodIcon; GUILayout.Label(icon); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("TPWeapon: "); icon = script.TPWeapon == null ? badIcon : goodIcon; GUILayout.Label(icon); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("Gun Info: "); icon = script.WeaponInfo == null ? badIcon : goodIcon; GUILayout.Label(icon); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndVertical(); } }
public void Export() { if (FPWeapon == null || Player == null) { return; } bl_GunInfo info = bl_GameData.Instance.GetWeapon(FPWeapon.GunID); GameObject root = new GameObject(info.Name + " [Export]"); root.transform.SetAsLastSibling(); bl_WeaponExported export = root.AddComponent <bl_WeaponExported>(); export.WeaponInfo = info; GameObject fpwCopy = Instantiate(FPWeapon.gameObject); fpwCopy.SetActive(true); fpwCopy.name = info.Name + " [FP]"; fpwCopy.transform.parent = root.transform; fpwCopy.transform.localPosition = Vector3.zero; export.FPWeapon = fpwCopy.GetComponent <bl_Gun>(); export.FPWPosition = FPWeapon.transform.localPosition; export.FPWRotation = FPWeapon.transform.localRotation; if (NetworkGun != null) { GameObject tpwCopy = Instantiate(NetworkGun.gameObject); tpwCopy.SetActive(true); tpwCopy.name = info.Name + " [TP]"; tpwCopy.transform.parent = root.transform; tpwCopy.transform.localPosition = Vector3.zero; export.TPWeapon = tpwCopy.GetComponent <bl_NetworkGun>(); export.TPWPosition = NetworkGun.transform.localPosition; export.TPWRotation = NetworkGun.transform.localRotation; } string lastPath = EditorPrefs.GetString(LAST_PATH, ""); Debug.Log(lastPath); string path = EditorUtility.OpenFolderPanel("Select Folder to save prefab", lastPath, ""); if (!string.IsNullOrEmpty(path)) { string relativepath = "Assets" + path.Substring(Application.dataPath.Length); relativepath += "/" + root.name + ".prefab"; #if UNITY_2018_3_OR_NEWER GameObject newPrefab = PrefabUtility.SaveAsPrefabAsset(root, relativepath); #else GameObject newPrefab = PrefabUtility.CreatePrefab(relativepath, root); #endif Selection.activeGameObject = newPrefab; EditorGUIUtility.PingObject(newPrefab); DestroyImmediate(root); EditorPrefs.SetString(LAST_PATH, path); } else { Debug.LogWarning("Could not create a prefab automatically."); Selection.activeGameObject = root; EditorGUIUtility.PingObject(root); } Close(); }
public void Import() { GameObject playerInstance = Player.gameObject; bool isPrefab = false; if (Player.gameObject.scene.name == null) { playerInstance = PrefabUtility.InstantiatePrefab(Player.gameObject, EditorSceneManager.GetActiveScene()) as GameObject; #if UNITY_2018_3_OR_NEWER PrefabUtility.UnpackPrefabInstance(playerInstance, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); #endif playerInstance.name = Player.gameObject.name; isPrefab = true; } bl_WeaponExported we = WeaponToImport; if (WeaponToImport.gameObject.scene.name == null) { GameObject weo = PrefabUtility.InstantiatePrefab(WeaponToImport.gameObject, EditorSceneManager.GetActiveScene()) as GameObject; #if UNITY_2018_3_OR_NEWER PrefabUtility.UnpackPrefabInstance(weo, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); #endif we = weo.GetComponent <bl_WeaponExported>(); } if (we.WeaponInfo != null && !string.IsNullOrEmpty(we.WeaponInfo.Name)) { if (!bl_GameData.Instance.AllWeapons.Exists(x => x.Name == we.WeaponInfo.Name)) { bl_GameData.Instance.AllWeapons.Add(we.WeaponInfo); we.FPWeapon.GunID = bl_GameData.Instance.AllWeapons.Count - 1; EditorUtility.SetDirty(bl_GameData.Instance); results[0] = 1; } else { we.FPWeapon.GunID = bl_GameData.Instance.AllWeapons.FindIndex(x => x.Name == we.WeaponInfo.Name); EditorUtility.SetDirty(bl_GameData.Instance); results[0] = 2; } } else { results[0] = 0; } bl_GunManager gm = playerInstance.GetComponentInChildren <bl_GunManager>(); if (gm != null) { we.FPWeapon.transform.parent = gm.transform; we.FPWeapon.transform.localPosition = we.FPWPosition; we.FPWeapon.transform.localRotation = we.FPWRotation; we.FPWeapon.name = we.FPWeapon.name.Replace("[FP]", ""); gm.AllGuns.Add(we.FPWeapon); results[1] = 1; } else { results[1] = 0; } if (we.TPWeapon != null) { we.TPWeapon.LocalGun = we.FPWeapon; we.TPWeapon.transform.parent = playerInstance.GetComponent <bl_PlayerSync>().NetworkGuns[0].transform.parent; we.TPWeapon.transform.localPosition = we.TPWPosition; we.TPWeapon.transform.localRotation = we.TPWRotation; we.TPWeapon.name = we.TPWeapon.name.Replace("[TP]", ""); playerInstance.GetComponent <bl_PlayerSync>().NetworkGuns.Add(we.TPWeapon); results[2] = 1; } else { results[2] = 0; } if (isPrefab) { } DestroyImmediate(we.gameObject); EditorUtility.SetDirty(playerInstance); EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); isDone = true; Repaint(); }
void ImportUI() { if (!isDone) { bool canImport = true; if (WeaponToImport == null) { EditorGUILayout.HelpBox("Assign the weapon exported prefab to import.", MessageType.Info); WeaponToImport = EditorGUILayout.ObjectField("Weapon To Import", WeaponToImport, typeof(bl_WeaponExported), true) as bl_WeaponExported; canImport = false; } else { GUILayout.Space(10); GUI.enabled = false; GUILayout.BeginHorizontal(); EditorGUILayout.ObjectField("Weapon To Import", WeaponToImport, typeof(bl_WeaponExported), true); GUILayout.Label(goodTexture, GUILayout.Width(15)); GUILayout.EndHorizontal(); GUI.enabled = true; } if (Player == null) { EditorGUILayout.HelpBox("Assign the player prefab where you wanna import this weapon.", MessageType.Info); Player = EditorGUILayout.ObjectField("Player", Player, typeof(bl_PlayerSync), true) as bl_PlayerSync; canImport = false; } else { GUI.enabled = false; GUILayout.BeginHorizontal(); EditorGUILayout.ObjectField("Player", Player, typeof(bl_PlayerSync), true); if (Player.gameObject.scene.name == null) { GUILayout.Label(EditorGUIUtility.IconContent("PrefabNormal Icon").image, GUILayout.Width(15)); } else { GUILayout.Label(EditorGUIUtility.IconContent("PrefabModel Icon").image, GUILayout.Width(15)); } GUILayout.Label(goodTexture, GUILayout.Width(15)); GUILayout.EndHorizontal(); GUI.enabled = true; } if (WeaponToImport != null && WeaponToImport.WeaponInfo != null && WeaponToImport.WeaponInfo.GunIcon != null) { GUILayout.Space(25); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); Texture2D icon = WeaponToImport.WeaponInfo.GunIcon.texture; float aspet = 100f / (float)icon.width; GUILayout.Space(50); GUILayout.Label(icon, GUILayout.Width(100), GUILayout.Height(icon.height * aspet)); GUILayout.Space(10); GUILayout.Label(EditorGUIUtility.IconContent("TimelineContinue").image, GUILayout.Width(20)); GUILayout.Space(10); GUILayout.Label(Player.gameObject.name, GUILayout.Width(150)); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } GUI.enabled = canImport; if (GUI.Button(new Rect(0, Screen.height - 40, Screen.width, 20), "IMPORT", EditorStyles.toolbarButton)) { Import(); } GUI.enabled = true; } else { GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("WEAPON IMPORTED!"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Weapon Info"); Texture icon = stateIcons[results[0]]; GUILayout.Label(icon, GUILayout.Width(20)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("FP Weapon"); icon = stateIcons[results[1]]; GUILayout.Label(icon, GUILayout.Width(20)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("TP Weapon"); icon = stateIcons[results[2]]; GUILayout.Label(icon, GUILayout.Width(20)); GUILayout.EndHorizontal(); GUILayout.Space(20); if (GUILayout.Button("DONE")) { Close(); } } }