- Home /
How to use an Image(PNG) and GUISkin inside a dll ?
Hi every one, I made a tool with a custom editor (buttons, frames, GUISkin, etc.) and compiled my tool into a dll (with Monodevelop), but, when I try to put the guiskin and the images, I can't use that inside the dll, I'm new about dll compilation and I don't understand many things, even I find some info about how to put and use resources into a dll but I get some errors like: can't convert -System.IO- to -Unityengine.Texture2D-, any help will be appreciated, thanks in advance.
Code Sample:
using System;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Reflection;
//
public class MyTool : EditorWindow
{
Texture2D images;
GUISkin mySkin;
Assembly myAssembly;
Stream imageStream;
//
static void Init()
{
// Draw the window.
}
//
void OnEnable ()
{
myAssembly = Assembly.GetExecutingAssembly();
images = myAssembly.GetManifestResourceStream("Images.Data.Button.png");
}
void OnGUI ()
{
GUI.skin = mySkin; // Here, I don't know how to search for the *.guiskin file inside the dll.
GUI.Button(Rect(0, 0, 32, 32), images);
}
}
Answer by Bunny83 · Mar 20, 2012 at 05:52 PM
Unity uses their own AssetDatabase to handle all kind of assets, in the editor as well as at runtime. Things like GUISkins are stored in a custom format and can't be serialized into a custom format. Assets can only be used when they are inside the Assets folder.
However, you can load external images (only jpg and png) with Texture2D.LoadImage. All other assets need to be imported into the assetdatabase by Unity. From an editor script you can use the AssetDatabase to access and use any asset in the project.
If you made a custom editor you usually create a unitypackage by exporting all relevant stuff. When the user imports the package, all contained assets will be at the same relative position inside the Assets folder.
Compiling editor extensions into a .NET assembly is quite common but not necessary. Well it helps to protect the scripts from being altered accidentally. It does not save the code from being copied by others. .NET assemblies can easily decompiled since that's even a feature of the language itself.
Thanks for your reply, but unfortunately that's not what I need, I already do something like that ($$anonymous$$aterial Animator in AssetStore), but what I want to do now it's something like the Play$$anonymous$$akerEditor Dll by Hutong Games. Anyway thanks again for your reply
It's not about what you need, it's about what is possible. A GUISkin needs the reference information of the Texture2D assets that should be used. This information is tracked by the AssetDatabase. You can't just save this information manually.
See this discussion of the Play$$anonymous$$aker author about the issue.
I don't have used the Playmaker yet, but they might have build the GUIStyles on-the-fly (like AngryAnt said) since you can't load them from outside the AssetDatabase.
If you have purchased Play$$anonymous$$aker, just use ILSpy and check out how they handle it.
btw. I don't get how your $$anonymous$$aterial Animator is related to this problem. Isn't it a runtime component?
$$anonymous$$ore or Less, if you check the package of my tool there's an image (Logo) like Asset, but if you check the playmaker package you will see no images like buttons, frames and logo, you can check a preview of the package content.
I don't want to spread secret code, but i've took a look into the playmakereditor.dll. Like i've guessed above they create every GUIContent on the fly like this:
FsmEditorStyles.LeftArrow = FsmEditorUtility.LoadDllResource("leftArrow", 23, 14);
The most important lines from LoadDllResource is like this:
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream manifestResourceStream = executingAssembly.Get$$anonymous$$anifestResourceStream("HutongGames.Play$$anonymous$$akerEditor.Assets.Play$$anonymous$$aker.Editor.Resources." + resourceName + ".png");
if (manifestResourceStream != null)
{
texture2D.LoadImage(FsmEditorUtility.ReadToEnd(manifestResourceStream));
manifestResourceStream.Close();
}
Like i said, you can't load Unity specific classes (GUISkin, $$anonymous$$esh, ...) from a byte-stream since they don't have any common deserialization methods. Images can be loaded, but only manually like this.
Your answer
Follow this Question
Related Questions
Import C# project into my Unity solution 1 Answer
error issues : KeyNotFoundException 1 Answer
Running pdb2mdb.exe as a MonoDevelop 2 Answers
Unhandled Exception. We Hate Them, But They Happen 0 Answers