Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- using System.Collections.Generic;
- public class SetBuildWindow : EditorWindow
- {
- #region Private
- private static Rect m_BuildBtn = new Rect(0,100,50,50);
- private static Rect m_GridBtn = new Rect(0, 0, 200, 50);
- private static Rect m_Window = new Rect(0,0,600,400);
- private int m_selInd = 0;
- private string[] m_aNames = {"Client","Server"};
- private const string MATERIAL_PATH = "RawResources/Materials/";
- private const string TEXTURE_PATH = "RawResources/Textures/";
- private const string GESTURE_SRC_PATH = "Assets/RawResources/Templates";
- private const string GESTURE_DST_PATH = "Assets/Resources/Templates";
- private const string PREFAB_SRC_PATH = "Assets/RawResources/Prefabs";
- private const string PREFAB_DST_PATH = "Assets/Resources/Prefabs";
- private const string DEF_CLIENT = "GAME_CLIENT";
- private const string DEF_SERVER = "GAME_SERVER";
- void Awake()
- {
- m_Window.x = (Screen.width - m_Window.width)/2;
- m_Window.y = (Screen.height - m_Window.height)/2;
- m_GridBtn.x = (Screen.width - m_GridBtn.width)/2;
- m_GridBtn.y = (Screen.height - m_GridBtn.height)/2;
- m_BuildBtn.x = m_Window.width - m_BuildBtn.width;
- m_BuildBtn.y = m_Window.height - m_BuildBtn.height;
- }
- void ManageResources()
- {
- switch(m_selInd)
- {
- case 0:
- {
- CopyRawResources(false);
- Debug.LogWarning("Build CLIENT");
- PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, DEF_CLIENT +";"+ DEF_SERVER);
- PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iPhone, DEF_CLIENT +";"+ DEF_SERVER);
- }
- break;
- case 1:
- {
- CopyRawResources(true);
- AssetDatabase.Refresh();
- Debug.LogWarning("Build SERVER");
- PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, DEF_SERVER);
- PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iPhone, DEF_SERVER);
- }
- break;
- default: break;
- }
- }
- void CopyRawResources(bool isServer)
- {
- FileUtil.ReplaceDirectory(PREFAB_SRC_PATH, PREFAB_DST_PATH);
- if (!isServer)
- FileUtil.ReplaceDirectory(GESTURE_SRC_PATH, GESTURE_DST_PATH);
- AssetDatabase.Refresh();
- if (isServer)
- {
- List<Object> assets = new List<Object>(Resources.LoadAll("Prefabs/"));
- for (int i=0; i<assets.Count; i++)
- {
- Object obj = assets[i];
- if (obj is GameObject)
- {
- CheckAllHierarchy(obj as GameObject);
- }
- }
- }
- }
- bool isMarkedForDestroying(Component cmp)
- {
- if (cmp is MeshRenderer ||
- cmp is MeshFilter ||
- cmp is ParticleSystem ||
- cmp is Light ||
- cmp is GUIText ||
- cmp is GUITexture ||
- cmp is Animation ||
- cmp is SkinnedMeshRenderer ||
- cmp is AudioListener ||
- cmp is AudioSource ||
- cmp is Animator ||
- cmp is CFX_ShurikenThreadFix ||
- cmp is SkinnedMeshRenderer ||
- cmp is ParticleAnimator ||
- cmp is ParticleEmitter ||
- cmp is Renderer ||
- cmp is TrailRenderer)
- {
- return true;
- }
- return false;
- }
- void CustomDestruction(GameObject obj)
- {
- Debug.Log ("Custom destruction: " + obj.name);
- if (obj.transform != null && obj.transform.parent == null)
- {
- Object prefab = PrefabUtility.GetPrefabObject(obj);
- string assetPath = AssetDatabase.GetAssetPath(prefab);
- AssetDatabase.DeleteAsset(assetPath);
- }
- }
- void FindAndDestroyComp(GameObject obj)
- {
- Component[] aComp = obj.GetComponents<Component>();
- if (aComp != null)
- {
- for (int j=0; j<aComp.Length; j++)
- {
- if (aComp[j] is Camera)
- {
- CustomDestruction(obj);
- break;
- }
- if (isMarkedForDestroying(aComp[j]))
- Component.DestroyImmediate(aComp[j], true);
- }
- if (obj != null)
- {
- aComp = obj.GetComponents<Component>();
- if (aComp != null && aComp.Length < 2 && obj.transform.parent != null)
- DestroyImmediate(obj, true);
- }
- }
- else
- Debug.Log ("Check object, smth is wrong: " + obj.name);
- }
- void CheckAllHierarchy(GameObject obj)
- {
- int childCount = obj.transform.childCount;
- if (childCount > 0)
- {
- for (int i=0; i<childCount; i++)
- {
- Transform trs = null;
- try
- {
- trs = obj.transform.GetChild(i);
- }
- catch(UnityException e)
- {
- Debug.Log (e);
- }
- if (trs!= null && trs.gameObject != null)
- {
- CheckAllHierarchy(trs.gameObject);
- if (trs==null && (i < childCount-1))
- {
- i--;
- childCount = obj.transform.childCount;
- }
- }
- }
- }
- FindAndDestroyComp(obj);
- }
- #endregion
- [MenuItem("UnnyEdit/SetBuild")]
- static public void BuildWindow()
- {
- EditorWindow.GetWindowWithRect<SetBuildWindow>(m_Window);
- }
- public void OnGUI()
- {
- m_selInd = GUI.SelectionGrid(m_GridBtn, m_selInd, m_aNames, 2);
- if (GUI.Button(m_BuildBtn, "Build"))
- {
- ManageResources();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement