How to use a static function(?)

I need to rotate a Texture2D and I have found the following chunk of code (from here), but I am unfamiliar with how to use static functions, I have tried RotateImage(texName, 45); and nothing appears to happen…

Could someone help me with this please?

public static Texture2D RotateImage(Texture2D originTexture, int angle)
    {
        Texture2D result;
        result = new Texture2D(originTexture.width, originTexture.height);
        Color32[] pix1 = result.GetPixels32();
        Color32[] pix2 = originTexture.GetPixels32();
        int W = originTexture.width;
        int H = originTexture.height;
        int x = 0;
        int y = 0;
        Color32[] pix3 = rotateSquare(pix2, (Math.PI / 180 * (double)angle), originTexture);
        for (int j = 0; j < H; j++)
        {
            for (var i = 0; i < W; i++)
            {
                //pix1[result.width/2 - originTexture.width/2 + x + i + result.width*(result.height/2-originTexture.height/2+j+y)] = pix2[i + j*originTexture.width];
                pix1[result.width / 2 - W / 2 + x + i + result.width * (result.height / 2 - H / 2 + j + y)] = pix3[i + j * W];
            }
        }
        result.SetPixels32(pix1);
        result.Apply();
        return result;
    }
    static Color32[] rotateSquare(Color32[] arr, double phi, Texture2D originTexture)
    {
        int x;
        int y;
        int i;
        int j;
        double sn = Math.Sin(phi);
        double cs = Math.Cos(phi);
        Color32[] arr2 = originTexture.GetPixels32();
        int W = originTexture.width;
        int H = originTexture.height;
        int xc = W / 2;
        int yc = H / 2;
        for (j = 0; j < H; j++)
        {
            for (i = 0; i < W; i++)
            {
                arr2[j * W + i] = new Color32(0, 0, 0, 0);
                x = (int)(cs * (i - xc) + sn * (j - yc) + xc);
                y = (int)(-sn * (i - xc) + cs * (j - yc) + yc);
                if ((x > -1) && (x < W) && (y > -1) && (y < H))
                {
                    arr2[j * W + i] = arr[y * W + x];
                }
            }
        }
        return arr2;
    }

Post the code that you use to call the function, so we can see exactly what you are doing, then we should be able to point out if you’ve made a mistake and how to correct it.

Thanks for the reply, I’m just trying:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class callRotate : MonoBehaviour {

    public rotateTexture rotateTexture;
    public Texture2D texName;

    private void Start()
    {
        rotateTexture.RotateImage(texName, 45);
    }

}

EDIT

So implementing the following changes have worked, no idea why but good-o!

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotateTexture : MonoBehaviour {

    public Texture2D tex;
    public Texture2D result;
    public Material mat;
    public texturePainter texturePainter;

    private void Start()
    {
        RotateImage(tex, 45);
    }

    public void RotateImage(Texture2D originTexture, int angle)
    {
        result = new Texture2D(originTexture.width, originTexture.height);
        Color32[] pix1 = result.GetPixels32();
        Color32[] pix2 = originTexture.GetPixels32();
        int W = originTexture.width;
        int H = originTexture.height;
        int x = 0;
        int y = 0;
        Color32[] pix3 = rotateSquare(pix2, (Math.PI / 180 * (double)angle), originTexture);
        for (int j = 0; j < H; j++)
        {
            for (var i = 0; i < W; i++)
            {
                //pix1[result.width/2 - originTexture.width/2 + x + i + result.width*(result.height/2-originTexture.height/2+j+y)] = pix2[i + j*originTexture.width];
                pix1[result.width / 2 - W / 2 + x + i + result.width * (result.height / 2 - H / 2 + j + y)] = pix3[i + j * W];
            }
        }
        result.SetPixels32(pix1);
        result.Apply();

        tex = result;
        texturePainter.brushTexture = result;
    }

    static Color32[] rotateSquare(Color32[] arr, double phi, Texture2D originTexture)
    {
        int x;
        int y;
        int i;
        int j;
        double sn = Math.Sin(phi);
        double cs = Math.Cos(phi);
        Color32[] arr2 = originTexture.GetPixels32();
        int W = originTexture.width;
        int H = originTexture.height;
        int xc = W / 2;
        int yc = H / 2;
        for (j = 0; j < H; j++)
        {
            for (i = 0; i < W; i++)
            {
                arr2[j * W + i] = new Color32(0, 0, 0, 0);
                x = (int)(cs * (i - xc) + sn * (j - yc) + xc);
                y = (int)(-sn * (i - xc) + cs * (j - yc) + yc);
                if ((x > -1) && (x < W) && (y > -1) && (y < H))
                {
                    arr2[j * W + i] = arr[y * W + x];
                }
            }
        }
        return arr2;
    }
}

The static keyword means that the item is available to callers at class scope, not object scope. This means you call the method on the class not on an object that you have created from that class.

If you remove line 7 (public rotateTexture rotateTexture;), then that should fix it for you.

Note: it is not a good idea to create an instance variable with the exact same name (including capitalisation) as the class. Try to keep your naming purposeful and distinct. Otherwise, it can cause confusion.

1 Like

I really need to study this further, I’m only comfortable with non-static functions at the moment, thank for your input :slight_smile:

1 Like

Static are basically global variable

Although the use of the phrase ‘global’ here probably needs clarifying slightly :). This is more the case only if they are public.

If marked protected or private, then the ‘global’ visibility is reduced, thus stretching the meaning of the word.

1 Like

I think static is a better term to use :stuck_out_tongue:

1 Like

Think about Vector3.Distance(a,b); or Color.red, or Mathf.Min(x1,x2);. The thing before the dot is just the path to find them – like the folder they’re in. Otherwise they’re normal functions and variables. That’s all a static is (all three of those are statics.)

It’s good to know since the Unity docs list things as static or public. In other words: normal, or member-function. For everything listed as static, you can see they’re complete. red is a static property in Color, which means red is red. You can’t write cc.red (for your color variable cc) and you wouldn’t want to. But greyScale isn’t a static, it’s not complete: Color.greyscale makes no sense. You want to use it like a member function: cc.greyscale.

1 Like