How to control canvase panels?

Hi all. I would like to find the best way to handle and control ui elements (canvas,panels,…) by implementing the most maintainable scripts. Should we have scripts for every ui elements? (button, panel,…) or only one script for every main panel (form)
refer me or link thx.

Control… how ?

1 Like

Yes, you will have to define what you mean by “control”.

Also, are you asking how to do it, or something else? Your question is very vague.

Should we have scripts for every ui elements? (button, panel,…) or only one script for every main panel (form)
it is clear!
I would like to know the best maintainable design patterns for UI scripts. For example I can write a general load scene button class that implements IClickable (OnClick function)

public class CLoadSceneButton:IClickable{
    [SerializeField]
    string sceneName;
    public void OnClick(){
          SceneManager.LoadScene(sceneName);
    }
}

I want to write clean codes. I saw the codes like unity ui tutorials. The scripts were very bad and messy without having a design(scripts that we can use them in one or few situations (very special cases))

For example control active/inactive panels,windows,…

public class PanelController{
   GameObject currentPanelObj;
   public void ChangeCurrentPanel(GameObject _newPanelObj){
currentPanelObj.SetActive(false);
currentPanelObj=newPanelObj;
_newPanelObj.SetActive(true);
   }
}

If your panels/dialogs/windows all share the same functionality you would try to use one script.
If they have different behavior, you would code each one individually.

You probably want to read up on object oriented programming basics a bit. If you already know that stuff - Unity does nothing to discourage object oriented techniques, so do what you feel is best. I can tell by your examples up above, that you probably want to have master parent classes that provide lots of functionality to lots of GameObjects. That’s a good approach.

thx so you say, every form should have a script like below:

public class CForm:Monobehaviour{
     Gameobject obj;
     Button ...
     Button ...
     Button ...
    Text ...
    Text ...
    Panel ...
     void Start(){

     }
     public void BuyClickButton(){}
     public void SellClickButton(){}
     public void ChangeClickColorButton(){}
     public void Scroll(){}
     public void ToggleButton(){}
}

it causes one big non maintainable/nontestable script! I want better way. For example you can not use the above script in another situation.(it is special form script)

public class CForm:Monobehaviour{
      
}
public class CBuyButton:Monobehaviour,IClickable{
      GameObject formObj;
      public void Initialize(Gameobject _formObj){
          formObj= _formObj;
      }
      public void OnClick(){
           //...
      }
}

Suppose we have the above code. we can replace it(for test function)
I would like to know more thx.

Your question is too broad to yield a substantial answer.

I feel Unity’s canvas UI is flexible enough that there is no need for a “design patterns” to come into play. Maintainability also depends on your personal preference when it comes to scripting.

If you prefer everything to be in one place, have a single script with everything your menus has to do, expose them as public methods, and assign them to the UnityEvents on the Canvas UI elements

If you prefer to segment up you functionality based on proximity of the UI element, then having separate scripts assigned to individual UI elements would better.

Depending on the complexity of your UI, both methods will come into play, so it becomes a matter of identifying which method fits the need of what particular part of your UI.

1 Like