폰 겜에서 빠질 수 없는 출석부 ..
부끄럽지만 1~31 까지 각각 Object의 이름을 이렇게 부여했다.
그리고 출석 아이템을 받지 않았을 때 현재 와 같이 갈색Sprite 가
출석 아이템을 받았다면 초록색 테두리가
받지 않고 지나쳤다면 회색 스프라이트로 변경된다..
때문에 각각 Late , TodayGet , Not Get 으로 명명하고 알맞는 Sprite를 넣어 줬다.
아래가 Control Attand Sprite 코드이다..
현재 Object의 Image Component를 가져왔다.
그리고 날짜에 맞게 잘 바꿔둔 현재 object의 이름을 가져온다.
이름은 String 이기 때문에 int.Parse 로 정수로 바꾸고 day 에 저장한다.
그리고 현재 날짜와 비교해서 만약에 이미 지난 날짜인데 출석 한적이 없다면 Sprite를 Late로
출석했다면 TodayGet으로 미래날짜라면 NotGet으로 변경한다..
여기서 AttandManager.AttandInstance.AttandDay는 AttandManager 스크립트에서 AttnadInstance를 Static으로 설정했다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using GameManager; public class ControlAttandSprite : MonoBehaviour { private Image CurrentSprite; [SerializeField] private Sprite Late; [SerializeField] private Sprite TodayGet; [SerializeField] private Sprite NotGet; // Use this for initialization void Start() { /* * AttandDay 에 대한 정보 복사 * */ CurrentSprite = GetComponent<Image>(); int day = int.Parse(gameObject.name); if (day < System.DateTime.Now.Day && AttandManager.AttandInstance.AttandDay[day - 1] == false) CurrentSprite.sprite = Late; else if (day < System.DateTime.Now.Day && AttandManager.AttandInstance.AttandDay[day - 1] == true) CurrentSprite.sprite = TodayGet; else CurrentSprite.sprite = NotGet; } // Update is called once per frame void Update () { } public void UpdateSprite() { CurrentSprite.sprite = TodayGet; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System.Collections; using System.Collections.Generic; using UnityEngine; using GameManager; public class AttandManager : MonoBehaviour { static protected AttandManager s_AttandInstance; static public AttandManager AttandInstance { get { return s_AttandInstance; } } public bool[] AttandDay = new bool[31]; // Use this for initialization void Awake() { s_AttandInstance = this; AttandDay[0] = true; AttandDay[3] = true; } void Start () { } // Update is called once per frame void Update () { } } | cs |
이렇게 Static 변수로 해줌으로써 한 씬에서 하나의 변수를 사용 할 수 있어서 다른 오브젝트와 상호 작용 할 수 있다.
마지막으로 수령완료를 누르면
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private ControlAttandSprite[] rewardSprite; [SerializeField] private GameObject rewards; // Use this for initialization void Start () { rewardSprite = rewards.GetComponentsInChildren<ControlAttandSprite>(); } // Update is called once per frame void Update () { } public void ChangingSprite() { rewardSprite[System.DateTime.Now.Day - 1].UpdateSprite(); AttandManager.AttandInstance.AttandDay[System.DateTime.Now.Day - 1] = true; } } | cs |
이 코드의 ChangingSprite가 실행되고 Sprite가 변경된다.
오늘이 27일이라 요렇게 된당!
'유니티' 카테고리의 다른 글
유니티에서 채팅기능 구현 및 문자열 송수신 문제점 (0) | 2019.05.02 |
---|---|
중간 발표, 피드백 (0) | 2018.05.29 |
UI OnOff Sprite Change (0) | 2018.05.27 |
UI : Canvas VS CameraScreen (0) | 2018.05.27 |