Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nj4242 · Nov 29, 2016 at 07:27 AM · texturespritepivotsprite editorimporter

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nj4242 · Nov 29, 2016 at 10:14 AM 0
Share

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. :)

avatar image aditya nj4242 · Nov 29, 2016 at 12:49 PM 0
Share

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 ^^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to create a sprite dynamically without provide a uv_position in code,for the reason that i have already made this in the sprite_editor. 0 Answers

OnPostprocessTexture TextureImporter.spritePivot doesn't seem to work. 1 Answer

Set Texture SpriteMode pivot point via script 0 Answers

Unity 2018 UGUI Android Sprite Textures Not Rendering Correctly 1 Answer

sphere texture rotation around pivot (eye) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges