- Home /
Assign Color[] var in prefabs using Editor Scripting
Here's the backstory of what I'm doing:
I have 3d models I'm importing and then placing them in prefabs.
Each side of my model has it's own color, which is UV mapped onto a texture.
Each prefab has a script that has a variable for Color[] array.
So far I've been manually adding the colors in the inspector for each side/color of my models.
Then I did some research into RaycastHit.textureCoord and Texture2D.GetPixelBilinear, and I've created a script that will raycast each side of my models and fill in the Color[] variable with the correct Color based on its texture's UVs.
The problem: this happens at playtime and I only need it done in the Editor while I'm building my game and before I publish it. What I need in the published build are the correctly assigned variables, that are pre-assigned in the Editor. I don't want to assign them during playtime.
QUESTION
So what I would like to do is convert my playtime script into something that will work as an Editor script. Where I can import my models, create my prefabs and assign the script that has Color[] variable. Then use an Editor script that will find the color of each side of the object and assign/save it to the prefab script.
Is this possible to do, and hopefully any examples/insight of how to do it? I've never done Editor scripting before.
Thanks!
Answer by DaveA · Jun 02, 2011 at 12:03 AM
Interesting! So I would use that in the 'main' script, and once it ran and assigned all my variables in the other scripts, I could safely delete the 'main' script and all the variables would stay assigned?
Is there also a way to do it as an Editor script/menu item as well? I found EditorUtility.ResetToPrefabState and .InstantiatePrefab, but nothing about assigning a variable and 'Applying' it back to the prefab.