duck

Unity sample assets cross platform initialise editor script

Apr 11th, 2014
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4.  
  5. [InitializeOnLoad]
  6. public class CrossPlatformInitialize {
  7.  
  8.     // Custom compiler defines:
  9.     //
  10.     // CROSS_PLATFORM_INPUT : denotes that cross platform input package exists, so that other packages can use their CrossPlatformInput functions.
  11.     // EDITOR_MOBILE_INPUT : denotes that mobile input should be used in editor, if a mobile build target is selected. (i.e. using Unity Remote app).
  12.     // MOBILE_INPUT : denotes that mobile input should be used right now!
  13.  
  14.     static CrossPlatformInitialize()
  15.     {
  16.         var defines = GetDefinesList(buildTargetGroups[0]);
  17.         if (!defines.Contains("CROSS_PLATFORM_INPUT"))
  18.         {
  19.             SetEnabled("CROSS_PLATFORM_INPUT",true,false);
  20.             SetEnabled("MOBILE_INPUT",true,true);
  21.         }
  22.     }
  23.    
  24.     [MenuItem("Mobile Input/Enable")]
  25.     static void Enable()
  26.     {
  27.  
  28.         SetEnabled("MOBILE_INPUT",true,true);
  29.         switch (EditorUserBuildSettings.activeBuildTarget)
  30.         {
  31.             case BuildTarget.Android:
  32.             case BuildTarget.iPhone:
  33.             case BuildTarget.WP8Player:
  34.             case BuildTarget.BlackBerry:
  35.             EditorUtility.DisplayDialog("Mobile Input","You have enabled Mobile Input. You'll need to use the Unity Remote app on a connected device to control your game in the Editor.","OK");
  36.             break;
  37.  
  38.         default:
  39.             EditorUtility.DisplayDialog("Mobile Input","You have enabled Mobile Input, but you have a non-mobile build target selected in your build settings. The mobile control rigs won't be active or visible on-screen until you switch the build target to a mobile platform.","OK");
  40.             break;
  41.         }
  42.     }
  43.  
  44.     [MenuItem("Mobile Input/Enable", true)]
  45.     static bool EnableValidate()
  46.     {
  47.         var defines = GetDefinesList(mobileBuildTargetGroups[0]);
  48.         return !defines.Contains("MOBILE_INPUT");
  49.     }
  50.  
  51.     [MenuItem("Mobile Input/Disable")]
  52.     static void Disable()
  53.     {
  54.         SetEnabled("MOBILE_INPUT",false,true);
  55.         switch (EditorUserBuildSettings.activeBuildTarget)
  56.         {
  57.         case BuildTarget.Android:
  58.         case BuildTarget.iPhone:
  59.         case BuildTarget.WP8Player:
  60.         case BuildTarget.BlackBerry:
  61.             EditorUtility.DisplayDialog("Mobile Input","You have disabled Mobile Input. Mobile control rigs won't be visible, and the Cross Platform Input functions will always return standalone controls.","OK");
  62.             break;     
  63.         }
  64.        
  65.        
  66.     }
  67.     [MenuItem("Mobile Input/Disable", true)]
  68.     static bool DisableValidate()
  69.     {
  70.         var defines = GetDefinesList(mobileBuildTargetGroups[0]);
  71.         return defines.Contains("MOBILE_INPUT");
  72.     }
  73.  
  74.     static BuildTargetGroup[] buildTargetGroups = new BuildTargetGroup[]
  75.     {
  76.         BuildTargetGroup.Standalone,
  77.         BuildTargetGroup.WebPlayer,
  78.         BuildTargetGroup.Android,
  79.         BuildTargetGroup.iPhone,
  80.         BuildTargetGroup.WP8,
  81.         BuildTargetGroup.BlackBerry,
  82.     };
  83.    
  84.     static BuildTargetGroup[] mobileBuildTargetGroups = new BuildTargetGroup[]
  85.     {
  86.         BuildTargetGroup.Android,
  87.         BuildTargetGroup.iPhone,
  88.         BuildTargetGroup.WP8,
  89.         BuildTargetGroup.BlackBerry,
  90.     };
  91.    
  92.     static void SetEnabled(string defineName, bool enable, bool mobile)
  93.     {
  94.         //Debug.Log("setting "+defineName+" to "+enable);
  95.         foreach (var group in mobile ? mobileBuildTargetGroups : buildTargetGroups)
  96.         {
  97.             var defines = GetDefinesList(group);
  98.             if (enable)
  99.             {
  100.                 if (!defines.Contains(defineName))
  101.                 {
  102.                     defines.Add(defineName);
  103.                 } else {
  104.                     return;
  105.                 }
  106.             } else  {
  107.                 if (defines.Contains(defineName))
  108.                 {
  109.                     while (defines.Contains(defineName)) defines.Remove(defineName);
  110.                 } else {
  111.                     return;
  112.                 }
  113.             }
  114.             string definesString = string.Join(";",defines.ToArray());
  115.             PlayerSettings.SetScriptingDefineSymbolsForGroup( group, definesString );
  116.         }
  117.     }
  118.    
  119.     static List<string> GetDefinesList(BuildTargetGroup group)
  120.     {
  121.         return new List<string>( PlayerSettings.GetScriptingDefineSymbolsForGroup( group ).Split(';'));
  122.     }
  123.  
  124.    
  125. }
Advertisement
Add Comment
Please, Sign In to add comment