How to create a Vector2-like custom class that uses strings instead of floats

Hi there,

I’d like to create a custom class which works like Vector2 but instead of floats it uses strings. Then I’d like to be able to edit strings via the inspector.

I can’t seem to get it working and to appear in the inspector though.

This is my HexColor2 class;

public class HexColor2
{
    public string h1;
    public string h2;
    public HexColor2(string _hex1, string _hex2)
    {
        h1 = _hex1;
        h2 = _hex2;
    }
}

And this is how I initiate but in the inspector my class doesn’t work like the Vector2 one.

public class PixelChanger : MonoBehaviour
{
        [SerializeReference] HexColor2> hexColors;
        [SerializeField] Vector2[] pos;
}

Thanks

You need to use the Serializable attribute to have classes show up in the Inspector. To get it to show horizontally like a Vector2, you probably would have to write your own OnInspectorGUI code.

3 Likes

Excellent. Thanks that worked. Thanks!

So now if I enter the hex values by the Inspector I want to inside the custom class HexColor2 add a # before the string. But the following doesn’t work. Do you know why?

[System.Serializable]
public class HexColor2
{
public string h1;
public string h2;

public HexColor2()
{
h1 = '#' + h1;
h2 = '#' + h2;
}

}

Explain what you’re doing there.

To me it looks like you wrote a constructor which will take h1 (which is null) and turn it into “#” + h1 which will throw a Null Reference Exception

Are you perhaps thinking about writing a custom property inspector that prefixes it with a # ?

1 Like

Cool. Thanks for replying.

So what I’m doing is entering the hex strings copied from an online pallette creator into the inspector - see screenshot. I wanted after I put these hex strings into the inspector, somehow a # was automatically added. But I can’t automatically add the #. When I print the hex numbers in the method ChangePixelColors() I get the correct hex strings but with the # - I hope this makes sense.

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

public class PixelChanger : MonoBehaviour
{
[System.Serializable]
public class HexColor2
{
public string h1;
public string h2;

public HexColor2()
{
h1 = '#' + h1;
h2 = '#' + h2;
}
}

[SerializeField] HexColor2[ ] hexColors;

private Texture2D texture;
private Sprite sprite;

void Start()
{
sprite = gameObject.GetComponent<SpriteRenderer>().sprite;
if (sprite == null) print("ERROR: no sprite found");
texture = sprite.texture;
ResizeSpriteToScreen();
}

HexColor2 GetHex()
{
int max = (hexColors.Length);
int rand = Random.Range(0, max);
return hexColors[rand];
}

void ChangePixelColors(HexColor2 hex)
{
// Sprite to create is 1 x 2 (width x height)
print("Hex " + hex.h1 + " , " + hex.h2);
Color color1;
Color color2;
ColorUtility.TryParseHtmlString(hex.h1, out color1);
ColorUtility.TryParseHtmlString(hex.h2, out color2);

texture.SetPixel(0, 0, color1);
texture.SetPixel(0,1, color2);

texture.Apply();
sprite = Sprite.Create(texture,
new Rect(0, 0, 1, 2),
new Vector2(0.5f, 0.5f));
}
}

If you want the # prefix, prepend it when the value is written.

If you just want it displayed but not in the data, that’s probably going to be a custom property inspector.

Or else if it’s only that hex parser that needs it, do nothing of the above but just prepend it before passing it in for parsing.

1 Like

I’ll also add you can just make a public Color h1; field and serialize the color normally.

1 Like

That’s great. Thanks!!