- Home /
How to change the color of predefine portion of the texture?
Hello Everyone, I want to change the color of the predefine portion of the texture.
See the attached image.
I want to change the color of the portion which are in the Red Rectangle.
can anyone please help me?
Thanks Yashesh
Answer by doska · Apr 10, 2014 at 10:09 AM
You can mark texture as Sprite.multiple, and separate this part as sprite. After that - do everything that you want with it. Sprites can overlap, so, you can have big sprite - whole texture and small one - selected part. Actually, you can separate it dynamicly via TextureImporter.spriteSheet
Sounds good.But i don't have idea about Sprite. can you please provide me some example link for reference? Thanks for quick reply :)
In attachement screenshot, it's just you to see how it looks loke. Here is a script to work with sprite. Sprite system is new, so there is not much tutorials on it. But sprite it's just a Texture2D in different maner.
public static void PrepeareTexture2DAsSprite (string pathToTexture, bool isSpriteSingle = true, Sprite$$anonymous$$etaData[] spriteSheetData = null)
{
TextureImporter textureImporter = AssetImporter.GetAtPath (pathToTexture) as TextureImporter;
TextureImporterSettings textureImporterSettings = new TextureImporterSettings ();
textureImporter.textureType = TextureImporterType.Sprite;
textureImporterSettings.spritePixelsToUnits = 1.0f;
textureImporterSettings.mipmapEnabled = false;
textureImporterSettings.readable = true;
textureImporterSettings.maxTextureSize = 4096;
textureImporterSettings.textureFormat = TextureImporterFormat.ARGB32;
textureImporterSettings.filter$$anonymous$$ode = Filter$$anonymous$$ode.Point;
textureImporterSettings.wrap$$anonymous$$ode = TextureWrap$$anonymous$$ode.Clamp;
textureImporterSettings.npotScale = TextureImporterNPOTScale.None;
textureImporterSettings.readable = true;
textureImporter.SetTextureSettings (textureImporterSettings);
AssetDatabase.ImportAsset (pathToTexture, ImportAssetOptions.ForceUpdate);
AssetDatabase.Refresh ();
if (isSpriteSingle)
textureImporterSettings.sprite$$anonymous$$ode = (int) SpriteImport$$anonymous$$ode.Single;
else
{
textureImporterSettings.sprite$$anonymous$$ode = (int) SpriteImport$$anonymous$$ode.$$anonymous$$ultiple;
if (spriteSheetData != null)
{
textureImporter.spritesheet = spriteSheetData;
}
}
textureImporter.SetTextureSettings (textureImporterSettings);
AssetDatabase.ImportAsset (pathToTexture, ImportAssetOptions.ForceUpdate);
AssetDatabase.Refresh ();
}
Thanks a lot for for your valuable response :) but i am getting compile time error "he type or namespace name `Sprite$$anonymous$$etaData' could not be found. Are you missing a using directive or an assembly reference?" can you please help me for that ?
Sprite$$anonymous$$etaData it's UnityEditor namespace. Script, that i give you, it's Editor script. You can use
#if UNITY_EDITOR
...
#endif
to disable compiling editor part of script from behavior script.
Your answer
Follow this Question
Related Questions
Reverse of Texture.EncodeToEXR in unity? 0 Answers
Change material color at runtime!! 5 Answers
How to Make a Character Flicker? 1 Answer
GetPixel Returning Inaccurate Color 1 Answer
Would a color based game run better than a texture based game? 2 Answers