Skip to main content
Added some information from comments
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. 

When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be done when you have a good reason to calculate that property at runtime. And if

The same applies to layouting. When you havefeel the need to do a reasonlot of layouting by calculating positions and dimensions of UI elements via C# code, then that's usually a sign that you could benefit from learning more about the automatic layouting capabilities of UIElements. I have no idea what you are actually trying to assignachieve, but unless it's something really unusual, it probably can be done with the layout engine. When you have a calculated style propertyspecific use-case where you are unsure how it can be done automatically, please open a new question.

And if you want to more than onedefine templates for certain repeating UI elements, but don't want to use the visual elementeditor, then you can define the UI in XML code instead of C# code.

OK, but what if all of this fails and you really, really have to use C# to set certain UI properties on multiple elements? In that case it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. 

When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be done when you have a good reason to calculate that property at runtime.

The same applies to layouting. When you feel the need to do a lot of layouting by calculating positions and dimensions of UI elements via C# code, then that's usually a sign that you could benefit from learning more about the automatic layouting capabilities of UIElements. I have no idea what you are actually trying to achieve, but unless it's something really unusual, it probably can be done with the layout engine. When you have a specific use-case where you are unsure how it can be done automatically, please open a new question.

And if you want to define templates for certain repeating UI elements, but don't want to use the visual editor, then you can define the UI in XML code instead of C# code.

OK, but what if all of this fails and you really, really have to use C# to set certain UI properties on multiple elements? In that case it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);
deleted 4 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be the done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be the done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);
added 538 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be the done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via codea helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be the done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via code:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

First of all, you don't need a pointer here, because VisualElement is a class, which means that every variable of type VisualElement is already a reference. For example:

 VisualElement visualElement1 = new VisualElement();
 VisualElement visualElement2 = visualElement1;

Now both variables visualElement1 and visualElement2 point to the same object. When you make a change through one of those references, then the change will be visible in the other.

But this does not really solve the actual problem, which is a whole lot of redundant code. When you have a lot of objects which you want to have the same style, then it is a good idea to use a StyleSheet for that purpose. Setting visualElement.style.* directly via code should only be the done when you have a good reason to calculate that property at runtime. And if you have a reason to assign a calculated style property to more than one visual element, then it is a good way to do that via a helper-method:

public static void SetColorForTeam(VisualElement element, PlayerTeam team) {
    element.color = team.brightColor;
    element.backgroundColor = team.darkColor;
    element.borderLeftColor = team.brightColor;
    element.borderRightColor = team.brightColor;
    element.borderTopColor = team.brightColor;
    element.borderBottomColor = team.brightColor;
}

...
SetColorForTeam(nameTagLocal, localPlayer.team);
SetColorForTeam(scoreDisplayLocal, localPlayer.team);
SetColorForTeam(nameTagOpponent, opponentPlayer.team);
SetColorForTeam(scoreDisplayOpponent, opponentPlayer.team);
added 538 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344
Loading
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344
Loading