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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by wamballa, Sep 4, 2022.

  1. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    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;

    Code (CSharp):
    1. public class HexColor2
    2. {
    3.     public string h1;
    4.     public string h2;
    5.     public HexColor2(string _hex1, string _hex2)
    6.     {
    7.         h1 = _hex1;
    8.         h2 = _hex2;
    9.     }
    10. }
    And this is how I initiate but in the inspector my class doesn't work like the Vector2 one.

    Code (CSharp):
    1. public class PixelChanger : MonoBehaviour
    2. {
    3.         [SerializeReference] HexColor2> hexColors;
    4.         [SerializeField] Vector2[] pos;
    5. }
    Thanks
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    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.
     
    wamballa, Bunny83 and Kurt-Dekker like this.
  3. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    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;
    }

    }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    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 # ?
     
    wamballa likes this.
  5. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    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));
    }
    }
     

    Attached Files:

  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    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.
     
    wamballa likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    I'll also add you can just make a
    public Color h1;
    field and serialize the color normally.
     
    wamballa likes this.
  8. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    That's great. Thanks!!