Is There a decent way to get my attribute to work like this?

I would like to get my attribute to take in sprites but I’m of course having issues with the sprites needing to be const. Is there an easy way to make this happen or should I change the attribute to an interface or abstract class?(Leaning toward interface at this point)

here’s what I have currently:

    [System.AttributeUsage(System.AttributeTargets.Method)]
    public class UtilityButtonAttribute : System.Attribute{
        public Sprite buttonImage, highlightImage, pressedImage, disabledImage;

        public UtilityButtonAttribute(Sprite buttonImage){
            this.buttonImage = buttonImage;
        }
        public UtilityButtonAttribute(Sprite buttonImage, Sprite pressedImage){
            this.buttonImage = buttonImage;
            this.pressedImage = pressedImage;
        }
        public UtilityButtonAttribute(Sprite buttonImage, Sprite pressedImage, Sprite highlightImage){
            this.buttonImage = buttonImage;
            this.pressedImage = pressedImage;
            this.highlightImage = highlightImage;
        }
        public UtilityButtonAttribute(Sprite buttonImage, Sprite pressedImage, Sprite highlightImage, Sprite disabledImage){
            this.buttonImage = buttonImage;
            this.pressedImage = pressedImage;
            this.highlightImage = highlightImage;
            this.disabledImage = disabledImage;
        }
    }
    public class utilityButtonControl : MonoBehaviour {
        public const Sprite spr = Object.FindObjectOfType<utilityButtonControl>().spr2;
        public Sprite spr2;
        [UtilityButtonAttribute(spr)]
        public void o(){

        }
    }

sorry if the code has errors or is incomplete I just wanted to have enough to ask my question:p

I honestly can’t figure out what you’re trying to do with the attributes… Can you please explain their purpose?

the attribute would be picked up by the “utilityButtonControl” class then control would create a ui button and assign the sprites to the button. and then the button (when clicked) would call the method that the attribute is over using System.Assembly.GetExecuringAssembly().GetTypes/GetMethods().

Maybe an interface would be better.

its a project where the user can create and add scripts which is why I’m not assigning in the hierarchy/inspector

Why are you creating buttons through scripts instead of the inspector? Attributes are complete overkill for what I assume you’re trying to do…

sorry I modified post 3 which explains it

basically its an objectified code editor.(Most interesting project ive been on yet BTW)

You will most likely need to set up your attribute to use strings instead of sprites. You can then use the string as a key to return the actual sprite from a sprite resource collection(dictionary).

1 Like

Good Idea…

Why didn’t I think of that??? :slight_smile:

bytes Can be serialized (for project save) and its easy to create sprites from that byte data then the bytes can be referenced by string (using some clever code).

wow that solves a handful of things

Thank you