Advertisement
Guest User

SetBuildWindow

a guest
Apr 12th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class SetBuildWindow : EditorWindow
  7. {
  8.     #region Private
  9.     private static Rect m_BuildBtn = new Rect(0,100,50,50);
  10.  
  11.     private static Rect m_GridBtn = new Rect(0, 0, 200, 50);
  12.  
  13.     private static Rect m_Window = new Rect(0,0,600,400);
  14.  
  15.     private int m_selInd = 0;
  16.  
  17.     private string[] m_aNames = {"Client","Server"};
  18.  
  19.     private const string MATERIAL_PATH = "RawResources/Materials/";
  20.     private const string TEXTURE_PATH = "RawResources/Textures/";
  21.  
  22.     private const string GESTURE_SRC_PATH = "Assets/RawResources/Templates";
  23.     private const string GESTURE_DST_PATH = "Assets/Resources/Templates";
  24.  
  25.     private const string PREFAB_SRC_PATH = "Assets/RawResources/Prefabs";
  26.     private const string PREFAB_DST_PATH = "Assets/Resources/Prefabs";
  27.  
  28.     private const string DEF_CLIENT = "GAME_CLIENT";
  29.     private const string DEF_SERVER = "GAME_SERVER";
  30.    
  31.     void Awake()
  32.     {
  33.         m_Window.x = (Screen.width - m_Window.width)/2;
  34.         m_Window.y = (Screen.height - m_Window.height)/2;
  35.  
  36.         m_GridBtn.x = (Screen.width - m_GridBtn.width)/2;
  37.         m_GridBtn.y = (Screen.height - m_GridBtn.height)/2;
  38.  
  39.         m_BuildBtn.x = m_Window.width - m_BuildBtn.width;
  40.         m_BuildBtn.y = m_Window.height - m_BuildBtn.height;
  41.     }
  42.  
  43.     void ManageResources()
  44.     {
  45.         switch(m_selInd)
  46.         {
  47.             case 0:
  48.             {
  49.                 CopyRawResources(false);
  50.                 Debug.LogWarning("Build CLIENT");
  51.                
  52.                 PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, DEF_CLIENT +";"+ DEF_SERVER);
  53.                 PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iPhone, DEF_CLIENT +";"+ DEF_SERVER);
  54.  
  55.             }
  56.             break;
  57.  
  58.             case 1:
  59.             {
  60.                 CopyRawResources(true);
  61.                 AssetDatabase.Refresh();
  62.                 Debug.LogWarning("Build SERVER");
  63.  
  64.                 PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, DEF_SERVER);
  65.                 PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iPhone, DEF_SERVER);
  66.             }
  67.             break;
  68.  
  69.             default: break;
  70.         }
  71.     }
  72.  
  73.     void CopyRawResources(bool isServer)
  74.     {
  75.         FileUtil.ReplaceDirectory(PREFAB_SRC_PATH, PREFAB_DST_PATH);
  76.  
  77.         if (!isServer)
  78.             FileUtil.ReplaceDirectory(GESTURE_SRC_PATH, GESTURE_DST_PATH);
  79.  
  80.         AssetDatabase.Refresh();
  81.  
  82.         if (isServer)
  83.         {
  84.             List<Object> assets = new List<Object>(Resources.LoadAll("Prefabs/"));
  85.  
  86.             for (int i=0; i<assets.Count; i++)
  87.             {
  88.                 Object obj = assets[i];
  89.  
  90.                 if (obj is GameObject)
  91.                 {
  92.                     CheckAllHierarchy(obj as GameObject);
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     bool isMarkedForDestroying(Component cmp)
  99.     {
  100.         if (cmp is MeshRenderer ||
  101.             cmp is MeshFilter ||
  102.             cmp is ParticleSystem ||
  103.             cmp is Light ||
  104.             cmp is GUIText ||
  105.             cmp is GUITexture ||
  106.             cmp is Animation ||
  107.             cmp is SkinnedMeshRenderer ||
  108.             cmp is AudioListener ||
  109.             cmp is AudioSource ||
  110.             cmp is Animator ||
  111.             cmp is CFX_ShurikenThreadFix ||
  112.             cmp is SkinnedMeshRenderer ||
  113.             cmp is ParticleAnimator ||
  114.             cmp is ParticleEmitter ||
  115.             cmp is Renderer ||
  116.             cmp is TrailRenderer)
  117.         {
  118.             return true;
  119.         }
  120.         return false;
  121.     }
  122.  
  123.     void CustomDestruction(GameObject obj)
  124.     {
  125.         Debug.Log ("Custom destruction: " + obj.name);
  126.  
  127.         if (obj.transform != null && obj.transform.parent == null)
  128.         {
  129.             Object prefab = PrefabUtility.GetPrefabObject(obj);
  130.             string assetPath = AssetDatabase.GetAssetPath(prefab);
  131.             AssetDatabase.DeleteAsset(assetPath);
  132.         }
  133.     }
  134.  
  135.     void FindAndDestroyComp(GameObject obj)
  136.     {
  137.         Component[] aComp = obj.GetComponents<Component>();
  138.    
  139.         if (aComp != null)
  140.         {
  141.             for (int j=0; j<aComp.Length; j++)
  142.             {
  143.                 if (aComp[j] is Camera)
  144.                 {
  145.                     CustomDestruction(obj);
  146.                     break;
  147.                 }
  148.  
  149.                 if (isMarkedForDestroying(aComp[j]))
  150.                     Component.DestroyImmediate(aComp[j], true);
  151.             }
  152.  
  153.             if (obj != null)
  154.             {
  155.                 aComp = obj.GetComponents<Component>();
  156.  
  157.                 if (aComp != null && aComp.Length < 2 && obj.transform.parent != null)
  158.                     DestroyImmediate(obj, true);
  159.             }
  160.         }
  161.         else
  162.             Debug.Log ("Check object, smth is wrong: " + obj.name);
  163.     }
  164.  
  165.     void CheckAllHierarchy(GameObject obj)
  166.     {
  167.         int childCount = obj.transform.childCount;
  168.  
  169.         if (childCount > 0)
  170.         {
  171.             for (int i=0; i<childCount; i++)
  172.             {
  173.                 Transform trs = null;
  174.  
  175.                 try
  176.                 {
  177.                     trs = obj.transform.GetChild(i);
  178.                 }
  179.                 catch(UnityException e)
  180.                 {
  181.                     Debug.Log (e);
  182.                 }
  183.                 if (trs!= null && trs.gameObject != null)
  184.                 {
  185.                     CheckAllHierarchy(trs.gameObject);
  186.  
  187.                     if (trs==null && (i < childCount-1))
  188.                     {
  189.                         i--;
  190.                         childCount = obj.transform.childCount;
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.         FindAndDestroyComp(obj);
  196.     }
  197.  
  198.     #endregion
  199.  
  200.     [MenuItem("UnnyEdit/SetBuild")]
  201.     static public void BuildWindow()
  202.     {
  203.         EditorWindow.GetWindowWithRect<SetBuildWindow>(m_Window);
  204.     }
  205.  
  206.     public void OnGUI()
  207.     {
  208.         m_selInd = GUI.SelectionGrid(m_GridBtn, m_selInd, m_aNames, 2);
  209.  
  210.         if (GUI.Button(m_BuildBtn, "Build"))
  211.         {
  212.             ManageResources();
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement