Archive for category Unity3D

Unity3D Conditional Device Compilation By Scenes

Recently I posted at the Unity3D forum in regard to how one can create a single project and compile multiple device and platform builds within this context. My goal here is simply to reduce having multiple projects for a single Unity3d application.

On my second cup of coffee here and hope I make sense with my questions here.

After watching the fantastic Smuggle Truck design/build discussions here:
http://learnunity3d.com/2011/04/smug…chniques-talk/
I was perplexed by how they created the iPad/iPhone/Mac OS conditional compilation build all from a single project. Only those assets referenced for that build are baked into the final project that is then sent to XCode.

I’m aware of the preprocessor flags (http://unity3d.com/support/documenta…mpilation.html). I also understand how to use them.

I guess the hurdle(s) I haven’t figured out yet are the following.

1. How does one condition the build between UNITY_IPHONE and UNITY_IPAD? Note: there isn’t any UNITY_IPAD define that I’m aware of. Within the Unity editor it is not possible to pass in custom preprocessor defines globally. They are scoped per-script. I see the command line option of “-executeMethod …” (http://unity3d.com/support/documenta…Arguments.html) Perhaps that is the magic sauce?

2. How did they conditionally include only unique assets to that particular build? For example, Mac OS X textures would be a higher resolution than those for the iPad and iPhone. Perhaps, this will be answered in question one.

Anyone have any ideas or other forum links that I may have missed?
Note, I have seen this: http://forum.unity3d.com/threads/171…al+Compilation

Well here is a basic solution that is created by scene. By scene means that you have to create a different scene for each device but give them the same name. As seen in the first picture here.

Let me just state for the record that this solution would work perfectly but it is less than ideal. I am posting this to merely explain how to conditionally gather assets into a final deployment as well as to isolate resources per build. I believe the ideal solution would be to use asset bundles. I will cover that in a new blog entry in a few days/weeks as I figure it out as well.

I am going to provide a script at the bottom of this article and a zip file containing the magic mixture to easily integrate this beast into your project. Integrating this script is easy. Simply uncompress the zip file and place the “MonkeyPrism” folder into the root of your project.

Once you have the C-Sharp editor object installed you should be able to run the basic build process using the menu system or shortcut keys as documented within the C-Sharp code.

Download the file here.

Everyone: This script requires Unity Pro and it is setup to build for (iPhone, iPad, Mac OS Universal and Windows).
PC Users: Take note that I used the “/” in my script.

View Code CSHARP
using System;
using UnityEditor;
using UnityEngine;
 
/// <summary>
/// A C# build object demonstrating a dynamic build process for Unity 3D.
/// 
/// This script requires you to organize your scenes by device and platform as follows:
///		SCENES_LOC / {iphone, ipad, macos, pc} / {your scenes}
///		i.e.	Assets/Scenes/iphone/scene0.unity
///				Assets/Scenes/iphone/scene1.unity
/// 
/// 
/// Please note: this is not perfect and honestly, I'm looking into an asset bundle
/// solution as well here.  I just wanted to figure out if there was a better solution.
/// Version 1.0
/// April 29, 2011
/// License:  Free to use and modify.
/// </summary>
public class MonkeyPrismBuild {
 
	//TODO: place your core scenes location here.  (relative to the already loaded project)
	private const string SCENES_LOC = "Assets/Scenes/";
	//TODO: place your scenes into this array.
	private static string[] myScenes = { 
		"home.unity" }; /*, "levelselect.unity", 
		"game.unity", "credits.unity" };*/
 
	enum MP_BUILD_TYPE {
		iPhone = 0, iPad, MacOS, PC
	};
	/**
	 * Make sure uniqueBuildNames maps with the enum above and take note of the GetScenesByBuildType method.
	 * Also: make sure that these names match with directories under your SCENES_LOC.
	 */
	private static string[] uniqueBuildNames = {"iphone","ipad","macos","pc"};
 
	[MenuItem ("Tools/Build/iPhone #&i")] //press shift+alt+i
	public static void PerformBuildIPHONE() {
		Debug.Log("Building iPhone version");
		RunBuild(MP_BUILD_TYPE.iPhone);
	}
 
	[MenuItem ("Tools/Build/iPad #&p")] //press shift+alt+p
	public static void PerformBuildIPAD() {
		Debug.Log("Building iPad version");
		RunBuild(MP_BUILD_TYPE.iPad);
	}
 
	[MenuItem ("Tools/Build/Mac OS #&m")] //press shift+alt+m
	public static void PerformBuildMACOS() {
		Debug.Log("Building Universal Mac OS version");
		RunBuild(MP_BUILD_TYPE.MacOS);
	}
 
	[MenuItem ("Tools/Build/PC #&c")] //press shift+alt+c
	public static void PerformBuildPC() {
		Debug.Log("Building Windows PC version");
		RunBuild(MP_BUILD_TYPE.PC);
	}
 
	private static string[] GetScenesByBuildType(MP_BUILD_TYPE type, ref string buildDir) {
		int len = myScenes.Length;
		string[] scenes = new string[len];
 
		//string buildDir = null;
		switch (type) {
			case MP_BUILD_TYPE.iPad:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.iPad];
				break;
			case MP_BUILD_TYPE.iPhone:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.iPhone];
				break;
			case MP_BUILD_TYPE.MacOS:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.MacOS];
				break;
			case MP_BUILD_TYPE.PC:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.PC];
				break;
		} //end switch;
 
		if (buildDir == null || buildDir.Length < 1) 
			throw new Exception("Invalid MP_BUILD_TYPE::BuildType Specified.  Please visit the MPBuild.cs script to resolve.");
 
		for (int i=0; i < len; i++) {
			scenes[i] = SCENES_LOC + buildDir + "/" + myScenes[i];
		}
		return scenes;
	}
 
	private static void RunBuild(MP_BUILD_TYPE type) {
		string buildDir = null;
		string[] scenes = GetScenesByBuildType(type, ref buildDir);
		switch (type) {
			case MP_BUILD_TYPE.iPad:
			case MP_BUILD_TYPE.iPhone:
				BuildPipeline.BuildPlayer(scenes, buildDir, BuildTarget.iPhone, BuildOptions.None);
				break;
			case MP_BUILD_TYPE.MacOS:
				BuildPipeline.BuildPlayer(scenes, buildDir, BuildTarget.StandaloneOSXUniversal, BuildOptions.None);
				break;
			case MP_BUILD_TYPE.PC:
				BuildPipeline.BuildPlayer(scenes, buildDir, BuildTarget.StandaloneWindows, BuildOptions.None);
				break;
		} //end switch;
 
	}
}

,

No Comments

Unity 3D JavaScript and C# Scoping/Compiling Issue

This is a quick write-up regarding something as simple as getting JavaScript to interoperate properly with C# scripts in your Unity projects.

I ran into such an issue after purchasing EZ Gui via the Unity Asset Store.  Immediately upon purchasing you are prompted to “import” the project into your codebase.  I did this without issue.  Everything worked fine and dandy until my JavaScript code required referencing a SpriteText object.  This is when the following issue began happening:

Unity BCE0018: The name ‘SpriteText’ does not denote a valid type (‘not found’).

My folder structure was simply the following:

/scripts/myCameraScript.js  <– trying to reference the SpriteText

 

View Code JAVASCRIPT
var test : SpriteText;  //this line was throwing the error.

 

/EZ GUI/ <– default after purchasing and importing from the Unity Asset store.

I sat and read Unity compiling and scoping rules over and over located here.  On point 4, the statement reads “All scripts that are compiled in this step have access to all scripts in the first group (“Standard Assets”, “Pro Standard Assets” or “Plugins”).”

The solution is simple here. Take the EZ Gui code located in the “Plugins” folder and move it to the root.

/EZ Gui/Plugins/ <– move this to:

/Plugins/

Done. It works. Super simple.

Bonus Play, Rule of thumb (kind of no brainers really):

  1. Place all your plugins in the /Plugins/ folder.
  2. Place all your C# Code into the /Standard Assets/ folder.
  3. Place all your JavaScript code into /Scripts/ or any folder that is NOT named (“Standard Assets”, “Pro Standard Assets” or “Plugins”)

This will lead to success and less headaches.

, , , , ,

No Comments