AvatarToggle.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class AvatarToggle : MonoBehaviour
  7. {
  8. public Toggle avatartoggle;
  9. public RawImage image;
  10. private Texture tex;
  11. private string avatar;
  12. private void Start()
  13. {
  14. avatartoggle.onValueChanged.AddListener(ValueChange);
  15. ToggleGroup toggleGroup = transform.parent.GetComponent<ToggleGroup>();
  16. if (toggleGroup)
  17. {
  18. avatartoggle.group = toggleGroup;
  19. }
  20. if (!string.IsNullOrEmpty(this.avatar))
  21. {
  22. if (UserInfo.avatar == this.avatar)
  23. {
  24. avatartoggle.isOn = true;
  25. }
  26. }
  27. }
  28. private void ValueChange(bool isOn)
  29. {
  30. if (isOn)
  31. {
  32. Debug.Log("PP");
  33. if (tex != null)
  34. {
  35. UserSystem.swithAvatarAction?.Invoke(tex);
  36. UserInfo.avatar = this.avatar;
  37. UserInfo.textIcon = this.tex;
  38. }
  39. }
  40. }
  41. public void Init(Texture tex)
  42. {
  43. if (tex != null)
  44. {
  45. image.texture = tex;
  46. this.tex = tex;
  47. }
  48. }
  49. public void Init(string avatar)
  50. {
  51. if (!string.IsNullOrEmpty(avatar))
  52. {
  53. this.avatar = avatar;
  54. }
  55. }
  56. }