Advertisement
romi_fauzi

SwitchPart

Nov 5th, 2019
2,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Experimental.U2D.Animation;
  5. using UnityEngine.UI;
  6.  
  7. namespace Completed
  8. {
  9.     public class SwitchPart : MonoBehaviour
  10.     {
  11.         [SerializeField] BodyParts[] bodyParts;
  12.         [SerializeField] string[] labels;
  13.  
  14.         // Start is called before the first frame update
  15.         void Start()
  16.         {
  17.             for (int i = 0; i < bodyParts.Length; i++)
  18.             {
  19.                 bodyParts[i].Init(labels);
  20.             }
  21.         }
  22.     }
  23.  
  24.     [System.Serializable]
  25.     public class BodyParts
  26.     {
  27.         [SerializeField] Button button;
  28.         [SerializeField] SpriteResolver[] spriteResolver;
  29.         public int id;
  30.  
  31.         public SpriteResolver[] SpriteResolver { get => spriteResolver; }
  32.  
  33.         //method to init the button callback
  34.         public void Init(string[] labels)
  35.         {
  36.             button.onClick.AddListener(delegate { SwitchParts(labels); });
  37.         }
  38.  
  39.         //method that are going to be triggered by the button, and it will switch the sprites of each resolver list.
  40.         public void SwitchParts(string[] labels)
  41.         {
  42.             id++;
  43.             id = id % labels.Length;
  44.  
  45.             foreach (var item in spriteResolver)
  46.             {
  47.                 Debug.Log(item.GetCategory() + " " + labels[id] + " " + id);
  48.                 item.SetCategoryAndLabel(item.GetCategory(), labels[id]);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement