Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Quaternions

Discussion in 'Scripting' started by image28, Nov 3, 2014.

  1. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
     
    GarthSmith likes this.
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Don't like inverting the localscale of sprites in unity2d to flip them, messes with the rotations to much, so going to write a small editor extension that flips sprite sheets. Will post code here when it's done for anyone who is interested.
     
  3. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    To use it the texture needs to be flagged Read/Write in the inspector as an Advanced Texture

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. using UnityEditor;
    6.  
    7. public class flipImage : ScriptableWizard {
    8.    public Texture2D[] inputTexture = new Texture2D[1];
    9.    public string filenameInsert="-flipped";
    10.    private Texture2D outputTexture;
    11.    private Color[] textureData;
    12.    private Color[] outputData;
    13.  
    14.    // Use this for initialization
    15.    [MenuItem("Window/Flip Texture")]
    16.    public static void ShowWindow()
    17.    {
    18.      ScriptableWizard.DisplayWizard("Flip Texture(s)", typeof(flipImage), "Flip Texture(s)");
    19.    }
    20.  
    21.    void OnWizardCreate()
    22.    {
    23.  
    24.      for(int d=0; d<inputTexture.Length;d++)
    25.      {
    26.        // Get Texture data
    27.        textureData = inputTexture[d].GetPixels();
    28.        outputData = new Color[textureData.Length];
    29.  
    30.        // Flip the Texture
    31.        for(int y=0; y< inputTexture[d].height; y++)
    32.        {
    33.          for(int x=inputTexture[d].width-1; x > 1; x--)
    34.          {
    35.            outputData[((y*inputTexture[d].width)+(inputTexture[d].width-x))]=textureData[((y*inputTexture[d].width)+x)];
    36.          }
    37.        }
    38.  
    39.        // Write to a new Texture
    40.        outputTexture =  new Texture2D(inputTexture[d].width, inputTexture[d].height, inputTexture[d].format,false);
    41.        outputTexture.SetPixels(outputData);
    42.        outputTexture.Apply();
    43.        byte[] bytes = outputTexture.EncodeToPNG();
    44.        string infile=AssetDatabase.GetAssetPath(inputTexture[d]);
    45.        string outfile=infile.Insert(infile.Length-4,filenameInsert);
    46.        File.WriteAllBytes(outfile, bytes);
    47.      }
    48.  
    49.      AssetDatabase.Refresh();
    50.  
    51.    }
    52. }
    53.  
     
  4. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Now I've run into this issue, http://forum.unity3d.com/threads/changing-animation-sprites.213431/ ... currently going to try a swap the filenames of the spritesheets at run-time, just hoping unity's animation system references to the the individual sprites by name and not position.... or I'll have to modify the about asset to reorder the sprites within the spritesheet.
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    That isn't going to work.... FileUtils is a unityEditor function, and I don't know about moving files around in an android built....
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
  7. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Bit of a wasted effort.... Going to write a shader with a flip bool.... not the best shader programmer but should be able to mange it.... will be my 3rd modified shader based on unity2D's spriterenderer shader
     
  8. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Sorry... wasn't sure where to put it....
     
  9. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Basically trying to figure out a way to flip a sprite without using the inverted localScale trick as It makes the rotations/movement code too tedious to program.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    would you not just rotate the gameobject...
     
    image28 likes this.
  11. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457