- Home /
Generating GUISkin by code?
Hi I am trying to make my code into a dll, I managed to patch the png images into the solution, but as much as i understand, now i need to generate all the custom styles and the gui skin by the code myself, this is what i have :
public static GUISkin DarkSkin;
public static GUISkin LightSkin;
static GuiStyleUtility()
{
DarkSkin = GetDarkSkin();
LightSkin = GetLightSkin();
}
private static GUISkin GetDarkSkin()
{
GUISkin skin = ScriptableObject.CreateInstance<GUISkin>();
List<GUIStyle> styles = new List<GUIStyle>();
GUIStyle style = new GUIStyle
{
name = "ToolsViewBackground",
normal =
{
background = Resource.ViewBackNormalDark,
textColor = GraphicUtility.ColorFrom256(193, 193, 193, 75)
},
border = new RectOffset(2, 2, 2, 2),
fontSize = 18,
alignment = TextAnchor.MiddleCenter,
stretchWidth = true
};
styles.Add(style);
skin.customStyles = styles.ToArray();
skin.hideFlags = HideFlags.HideAndDontSave;
return skin;
}
}
As much as I understand, this should generate the Gui skin and assign it to the public constant DarkSKin and i should be able to access it outside of the static class, but, when i generate the custom editor window that is using this skin it yells that the custom style is not found, of course i triple checked the typing and there is no error there. so how can i make this work? or how can I make a gui skin totally by code myself?
actually let me correct myself, it is generating the gui now, but, the problem is that the editor view is behaving strange, you see, there is 0 warning, 0 error, but the editor window is not loading images at all, to make things worse, i can't see the skin in hierarchy and i can't even make sure my images are being loaded properly or not :/
When you die i cried very, very, very long!!!!! Why you didn`t escape????
Answer by DaiMangouDev · Oct 28, 2015 at 07:37 PM
ProjectWindowUtil.CreateNewGUISkin();
someone exposed the code here
or you can do this. which give you more control over creation. best method
GUISkin myGUISkin = new GUISkin();
myGUISkin.name = "MyGUISkin";
// set you box, button, slider ... values
// myGUISkin.box.hover.background = SOME TEXTURE
GUISkin gs = ScriptableObject.CreateInstance<GUISkin>();
gs = myGUISkin;
// create the new skin in the Assets folder
AssetDatabase.CreateAsset(gs, "Assets");
now , I must say ... our projects look eerily similar . I am working with light and dark skins for my project too.I found a great way to work with images in the dll. you want to get you want to read the font files from your dll next right ?
Answer by Jiraiyah · Oct 29, 2015 at 03:52 AM
@DMDev I figured out the problem with my code, can't believe a single typo passed me after 4 times of code check. and yes I tried to use fonts as resources in my project but I couldn't figure out a way that would accept the font as byte[] and produce the font within the code, you know something that may work? right now, I can only think about some static script that uses InitializeOnLoad to copy the font from resources and put it into a resources folder by using system.io not unity itself and i don't like that solution :/
You should post this reply as a comment to my answer. If i didn't check this question again I would never have known that you replied. Oh and if my answer is correct , you should accept it so that others know the answer too. As for fonts , I have a method that i'm working on that will get fonts from the resx but i haven't gotten any solid results yet.
But you can find a bunch of functions online that show how to convert byte arrays to objects
then you can try converting the object to a font . which is what i'm doing
Can we actually convert the .net object (not Unity.Object) to Unity Font??? hmm never thought about that one, i will do some research on both.net object and Unity.Object tomorrow, 2 point to u as reward for opening a new window for me there.
Actually I think I found a neat solution not only for font but anything that needs to be handled on unity side, take a look http://answers.unity3d.com/answers/437566/view.html with unity 5.x having all the features for free, this is no more problem, make asset bundles, add them to the dll project, stream it as bytes, instantiate it and get access to what ever is inside of it :D
interesting !, i never thought of using asset bundles. this WILL work :) really great. thanks for that!