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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Extending components and setting defaults

Discussion in 'Editor & General Support' started by NightmareTFD, Mar 2, 2016.

  1. NightmareTFD

    NightmareTFD

    Joined:
    Feb 11, 2015
    Posts:
    39
    Hello,

    we are finding we would like to set material/textures/etc on UI components when first added. We thought the best way to do this would be to
    1. extend the unity UI component
    2. on initial adding of component set some defaults for our artist.

    so for example we want a particular material to be the default material until an artist changes it.

    is there a way to set these items to defaults only when the component is first added?

    so for example
    class MyImage: Image {
    OnAddComponent() {
    material = Resouces.Load("GameDefaulyMaterial");
     
  2. NightmareTFD

    NightmareTFD

    Joined:
    Feb 11, 2015
    Posts:
    39
    Is there no way to do this or is it so simple that no one is replying?
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You can add a custom addcomponent entry that will change the values to your specification after creating it.
    Here is a example form the Unity UI extensions project

    Code (CSharp):
    1.     [MenuItem("GameObject/UI/Extensions/UI Flippable", false, 2003)]
    2.         static public void AddUIFlippableImage(MenuCommand menuCommand)
    3.         {
    4.             GameObject go = CreateUIElementRoot("UI Flippable", menuCommand, s_ImageGUIElementSize);
    5.             go.AddComponent<Image>();
    6.             go.AddComponent<UIFlippable>();
    7.             Selection.activeGameObject = go;
    8.         }
    https://bitbucket.org/ddreaper/unit...ns.cs?at=develop&fileviewer=file-view-default
    You can change it to do whatever you like as a preset.
    Your artist then uses the menu item defined by the MenuItem attribute to create a component with your preset.
     
  4. NightmareTFD

    NightmareTFD

    Joined:
    Feb 11, 2015
    Posts:
    39
    fffMalzbier,
    Thanks for the info. i'll definately do that. i was hoping there was a way to hook into the normal addcomponent method though, as while we can tell people to use the menu item there are other ways of adding components.