Duplicate Texture in Editor
TL;DR: Is there a way to duplicate a texture in memory ? I just want a duplicate version with filter set as Point instead of Bilinear. Only in the editor.
I made a simple UV editor for a procedural mesh I have. The UV editor simply displays the mesh renderer mainTexture scaled up and displays a few editable rects on it for each UV.
I want to have this texture be displayed in FilterMode.Point. I'm currently using EditorGUI.DrawTextureTransparent to display it. The only way I found was to modify the original texture and set it to point filtering which is not what I wanted. So I ended up temporarily duplicating the texture in my custom inspector.
If I do it like this :
MeshRenderer renderer = manager.GetComponent<MeshRenderer>();
Texture2D originalTexture = renderer.sharedMaterial.mainTexture as Texture2D;
texture = Instantiate(originalTexture) as Texture2D;
texture.filterMode = FilterMode.Point;
It works but I get this error in the console: Instantiating a non-readable 'Simple' texture is not allowed! Please mark the texture readable in the inspector or don't instantiate it.
I do it this way instead:
MeshRenderer renderer = manager.GetComponent<MeshRenderer>();
Texture2D originalTexture = renderer.sharedMaterial.mainTexture as Texture2D;
// temporarily switch texture to CPU read/write
string path = AssetDatabase.GetAssetPath(originalTexture);
TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(path);
importer.isReadable = true;
AssetDatabase.ImportAsset(path);
// copy texture localy with filtering removed
texture = new Texture2D(originalTexture.width, originalTexture.height, TextureFormat.RGBA32, false, false);
texture.SetPixels(originalTexture.GetPixels());
texture.filterMode = FilterMode.Point;
// remove CPU read/write
importer.isReadable = false;
AssetDatabase.ImportAsset(path);
No error in the console this time but very often the texture appears empty. And it constantly changes the texture's resource ID...
Another solution suggested on IRC was to use AssetDatabase.CopyAsset but that would require creating a temporary file which is quite overkill for my use case...
Is there any better way of duplicating a texture ? Or is there another way to have the texture be displayed Point sampled ?
Thanks for any help !
The way I do right now as another drawback : it keeps changing the resource ID of the texture unnecessarily...
Answer by msklywenn · Jan 16, 2016 at 05:46 PM
Just a found a way : simply use Texture.LoadTexture manually reading the bytes of the original file. This of course doesn't work for procedural textures though, but that might not be a real issue.
Your answer
Follow this Question
Related Questions
Change Texture 0 Answers
shader graph messes up the texture (2D) 0 Answers
In EditorGUI Texture2D is dark. 1 Answer
Applying one sprite/texture across multiple gameobjects. (2D) 0 Answers
Calculating normals in shader 0 Answers