- Home /
How to assign a GUISkin using code only?
Hi, I have made an editor script that creates an object with an assigned script. Unfortunately, that object will always have an empty GUISkin, so I have to assign it by inspector, which is really inefficient as I'm going to start using hundreds of these objects.
Is there anyway to assign a GUISkin without the use of the inspector, via javascript coding?
Like:
GUI.skin = GetAsset("MainSkin");
This actually helped me:
http://answers.unity3d.com/questions/153156/Global-GUISkin.html
If you actually have a simpler solution, just tell me.
Answer by TonyLi · Jun 07, 2013 at 01:46 AM
At edit-time, you can use Resource.LoadAssetAtPath(). Example:
mySkin = Resources.LoadAssetAtPath("Assets/MySkin", GUISkin) as GUISkin;
At run-time, put your GUISkin somewhere under any folder named Resources, and use Resources.Load(). Example:
GUI.skin = Resources.Load("Skins/MyGUISkin") as GUISkin;
In practice, check the return value for null first. The load could have failed.
It didn't work the first time, but for some reason, it worked! Thanks!
Your instance variable doesn't have a type!!!
private static var instance : SingletonSkin;
From http://docs.unity3d.com/Documentation/$$anonymous$$anual/android-GettingStarted.html
For performance reasons, dynamic typing in JavaScript is always turned off in Unity Android, as if #pragma strict were applied automatically to all scripts. This is important to know if you start with a project originally developed for the desktop platforms since you may find you get unexpected compile errors when switching to Android; dynamic typing is the first thing to investigate. These errors are usually easy to fix if you make sure all variables are explicitly typed or use type inference on initialization.
Provide explicit types of everything (e.g., "var i : int" ins$$anonymous$$d of just "var i")
You may need to typecast things (e.g,. GetComponent("Foo") as Foo)
Your answer
Follow this Question
Related Questions
What is GUID vs AssetPath? 2 Answers
Asset Importer, GUISkin Not Moved 0 Answers
Dynamic Asset loading? 6 Answers
How NOT to create redundant materials with new names on import? 2 Answers
Why Are My Project Assets Constantly Re-Importing? 2 Answers