How to change material or texture by script public field on Inspecor at edit mode?

How to change material or texture by script public field on Inspecor at edit mode?

public class test : MonoBehaviour 
{
	public enum TextureName
	{
		Texture1,
		Texture2
	}

	public TextureName textureName = TextureName.Texture1;
	public Texture2D[] textures;
#if UNITY_EDITOR
	void OnValidate()
	{
		// this can work but report "memory leak" error
		renderer.material.mainTexture = textures[(int)textureName];

		/*
		 * this changed all objects that use the material,not my purpose.
		 */ 
		//renderer.sharedMaterial.mainTexture = textures[(int)textureName];
	}
#endif
}

Hi, gays
the code is very simple, is there any way to achieve my purpose?

up

You might want to rephrase / add more detail to explain what it is you are trying to do. I know I’ve read this a couple of time since your original post and I’m not sure what it is you are trying to do or where it is you need help. Given the lack of replies I expect others are in the same situation…

Thanks a lot!
My purpose is very simple, display all changes in viewport when you changed the param in script’s inspector at edit mode.
imagin this, i have some fruit textures, then I create a fruit prefab with a script “fruit”.
I drag the prefab to scene , then set the type, and display in viewport. I don’t want to push play button, then get the result.
sorry for my pool english.

1363939--68053--$levelEntrance.jpg
it’s game level icon entrance. It’s a prefab. because the change “type” and “level” need use renderer.material… if at edit mode, materials leaks!