- Home /
Texture Import Settings problem!
Ahoy,
I want to change texture settings, apparently the pivot of the texture on runtime. But this code doesn't seems to work, (no errors just no change in pivot):
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Pivot Before : " + mySprite.pivot);
path = AssetDatabase.GetAssetPath(texture);
TextureImporter texImporter = AssetImporter.GetAtPath(path) as TextureImporter;
TextureImporterSettings texSettings = new TextureImporterSettings();
texImporter.ReadTextureSettings(texSettings);
texSettings.spriteAlignment = (int)SpriteAlignment.Center;
texSettings.spritePivot = new Vector2(0.5f, 0.5f);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
//AssetDatabase.WriteImportSettingsIfDirty(path);
Debug.Log("Pivot Now : " + mySprite.pivot);
}
The logs for both pivot now and before results the same, doesn't change anything. I used the following code from this thread.
It would be very helpful if someone points me where I am wrong.
Thanks.
Answer by aditya · Nov 29, 2016 at 08:15 AM
This is the code i use to automatically slice all sprites from a texture ... but you can use it to get a clue about how i imported the texture as a sprite and changed its pivot and other settings
i m sorry as i m pasting it as is without any stripping
OR YOU CAN SIMPLY CHECK THIS ANSWER
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
int index = path.LastIndexOf (".");
string ext = path.Substring (index+1);
if (index > 0 && ext.Equals ("xml")) {
XmlDocument xDoc = new XmlDocument ();
try{
xDoc.Load(path);
XmlElement xRoot = xDoc.DocumentElement;
float totalNodes = (float)xRoot.SelectNodes("descendant::*").Count;
float nodesProcessed = 0f;
float totalProgess = 0f;
Vector4 transfrmComponents = new Vector4 (0f, 0f, 0f, 0f);
XmlNodeList textureIteration = xDoc.GetElementsByTagName("texture"); // Iterate through every Image File get its name and create path
foreach(XmlNode iteratedTexture in textureIteration){
nodesProcessed++;
// Here i m importing my texture as sprite *******************************
string textureName = iteratedTexture.Attributes["name"].Value;
totalProgess = ((nodesProcessed/totalNodes) * 100f) / 100f;
EditorUtility.DisplayProgressBar("Slicing image(s)", "Slicing : " + textureName, totalProgess);
int pathNameIndex = path.LastIndexOf("/");
string pathToUse = path.Substring(0, pathNameIndex + 1) + textureName;
TextureImporterSettings tSettings = new TextureImporterSettings();
TextureImporter tI = AssetImporter.GetAtPath(pathToUse) as TextureImporter;
tI.ReadTextureSettings(tSettings);
tSettings.spriteAlignment = (int)SpriteAlignment.Custom;
tSettings.mipmapEnabled = false;
tI.SetTextureSettings(tSettings);
tI.isReadable = true;
tI.textureType = TextureImporterType.Sprite;
tI.spriteImportMode = SpriteImportMode.Multiple;
tI.spritePixelsPerUnit = 100f;
tI.maxTextureSize = 1024;
List<SpriteMetaData> newData = new List<SpriteMetaData>();
// Till Here ************************************
XmlNodeList spriteIteration = iteratedTexture.ChildNodes; // Iterate through every sprite in image and get its name and transforms
foreach(XmlNode iteratedSprite in spriteIteration){
nodesProcessed++;
totalProgess = ((nodesProcessed/totalNodes) * 100f) / 100f;
EditorUtility.DisplayProgressBar("Slicing image(s)", "Slicing : " + textureName, totalProgess);
string spriteName = iteratedSprite.Attributes["name"].Value;
XmlNodeList spriteTransforms = iteratedSprite.ChildNodes; // Iterate through every transform component of sprite and save them
foreach(XmlNode spriteInfo in spriteTransforms){
nodesProcessed++;
totalProgess = ((nodesProcessed/totalNodes) * 100f) / 100f;
EditorUtility.DisplayProgressBar("Slicing image(s)", "Slicing : " + textureName, totalProgess);
float res;
switch(spriteInfo.Name){
case "x":
float.TryParse(spriteInfo.InnerText, out res);
transfrmComponents.x = res;
break;
case "y":
float.TryParse(spriteInfo.InnerText, out res);
transfrmComponents.y = res;
break;
case "width":
float.TryParse(spriteInfo.InnerText, out res);
transfrmComponents.z = res;
break;
case "height":
float.TryParse(spriteInfo.InnerText, out res);
transfrmComponents.w = res;
break;
}
}
// Here i m saving my sprite meta data ********************************
SpriteMetaData smd = new SpriteMetaData();
smd.pivot = new Vector2(0.5f, 0.5f);
smd.alignment = 9;
smd.name = spriteName;
smd.rect = new Rect(transfrmComponents.x, transfrmComponents.y, transfrmComponents.z, transfrmComponents.w);
newData.Add(smd);
}
tI.spritesheet = newData.ToArray();
AssetDatabase.ImportAsset(pathToUse, ImportAssetOptions.Default);
// Till here ******************************************
}
EditorUtility.ClearProgressBar();
}catch(XmlException xEx){
if (EditorUtility.DisplayDialog ("Asset Load Failed", "Could not Load XML file : \n\n" + xEx.Message, "Ok"))
return;
else
return;
}
} else if (EditorUtility.DisplayDialog ("Unexpected Asset", "Need XML file to proceed", "Ok"))
return;
else
return;
Hey, @aditya, thanks for answering, but the code you gave is nothing close to my understanding, can you give me a general idea about it, about where should I use to set the pivot of my sprite. Thanks. And I did checked the thread earlier before the subject raised here. The basic idea of implementing pivots by parenting is such a good way, efficient too. I am actually using the parenting for pivoting purpose, and the same sprite I am using to change the pivot with the code in the question I provided. BECAUSE, I need to create multiple pivots so that whenever the object been interacted say "scaling" it does change dynamically from script, like pivot on bottom right when scaling -x, and pivot be in bottom left when scaling +x. Now, I would manage to scale either way, but I just need to create multiple pivots to use dyanmically, that's why I came up with overriding TextureImportSettings otherwise I'd be using pareting and stuffs to pivot any object. It would be helpful if you consider that, Thanks. :)
O$$anonymous$$AY ... if parenting is not working well for you then i would advice you to go with the third option of that thread .... i.e Sprite.Create ... it might be not that efficient but it will work for you, with this method, whenever you want to change your sprite either with scale or position, you had to create new sprite with Sprite.Create with new pivot position, i hope you are getting my point ... and my script will just work In-Editor, so you can ignore it, sry ^^