saraaaaras / Redo-YYY-New

0 stars 0 forks source link

Learn to use quick Local functions / delegate #4

Open PCRedHot opened 1 year ago

PCRedHot commented 1 year ago

https://github.com/saraaaaras/Redo-YYY-New/blob/7d2da933fc6a144d62eb12a0f94cc54c3e474cad/Assets/Inport/script/GameEngine.cs#L143-L163

This one I know you could not do it the right way, cuz its a hard one. But its time to take a look at more 'advanced' syntax. Here, you can create local function / delegate + dynamically assign listener to the Button. Dragging in unity's inspector is easy and fun, but also very limiting (given you don't want to touch the UnityEditor module which is very hard for you)

for (int i = 0; i < buttons.length; i++) {
  buttons[i].onClick.AddListener(delegate{ChangeDiceFaceToQMOrResult(i);});
  // OR
  buttons[i].onClick.AddListener(() => {ChangeDiceFaceToQMOrResult(i);});
}
PCRedHot commented 1 year ago

https://github.com/saraaaaras/Redo-YYY-New/blob/7d2da933fc6a144d62eb12a0f94cc54c3e474cad/Assets/Inport/script/GameEngine.cs#L205-L266

Same problem, very horrible if there are more buttons

PCRedHot commented 1 year ago

https://github.com/saraaaaras/Redo-YYY-New/blob/7d2da933fc6a144d62eb12a0f94cc54c3e474cad/Assets/Inport/script/GameEngine.cs#L442-L462

PCRedHot commented 1 year ago

https://github.com/saraaaaras/Redo-YYY-New/blob/7d2da933fc6a144d62eb12a0f94cc54c3e474cad/Assets/Inport/script/GameEngine.cs#L119

WOW! Acutally you have used it in your code!

But this is not a dictionary :) https://github.com/saraaaaras/Redo-YYY-New/blob/7d2da933fc6a144d62eb12a0f94cc54c3e474cad/Assets/Inport/script/GameEngine.cs#L117