- Home /
Create an asset and keep reference?
How could I do this:
Texture2D newTexture = AssetDatabase.CreateAsset (someTexture, "someDirectory/myTexture.asset");
When I tried that it says I can't convert void to texture2d, which I get that, but how can I keep the reference to the created asset? Like not "someTexture" but the new asset "myTexture"?
While using an editor tool, I need to generate a texture, save it to assets, then keep a reference to do more stuff, like assign to material and stuff.
Answer by Mikilo · Mar 29, 2015 at 06:35 AM
So, here is a solution:
Texture2D someTexture = new Texture2D(10, 10);
AssetDatabase.CreateAsset(someTexture, "someDirectory/myTexture.asset");
var asset = AssetDatabase.LoadMainAssetAtPath("someDirectory/myTexture.asset");
var go = GameObject.Find("GameObjectInScene"); // Find the GameObject in the scene.
go.GetComponent<YourScript>().myTexture = EditorUtility.InstanceIDToObject(asset.GetInstanceID()) as Texture2D;
I stay in touch.
Have a nice day. =D
Well I'm creating an asset, a texture that's been edited through an editor tool script, which unity will later "forget" the temporary copy that's being used to create this asset, but the asset I've created is permanent... so I'd like to reference it like the same way I would if I assigned it as a public texture to a script in the inspector. See what I mean?
If I say short:
You want to set a public Texture2D with a new created asset. Right?
If you often do trick in the Unity editor, you should give a look at those classes: EditorUtility EditorGUIUtility PrefabUtility EditorApplication
The best thing a developer could read. =D
I know from experience, that when you read something, even if you don't need it know, you know that it exists and you will remember it easily in the futur. That's why we have to read.