赞
踩
控制这一组数字的动作,
首先思路是单个数字入手,先能控制单个数字从0-9运行
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class NuberCtrl : MonoBehaviour
- {
- [Range(0,9)]
- public int CurNuber;
- public List<GameObject> nuberObjList = new List<GameObject>();
-
- public bool UpDateNuber = true;
- private void Update()
- {
- if (UpDateNuber) {
- SetNuber(CurNuber);
- }
- }
-
- public void SetNuber(int Count)
- {
- if (nuberObjList.Count <= 0) {
- return;
- }
- foreach (var item in nuberObjList)
- {
- item.SetActive(true);
- }
- if (Count == 9)
- {
-
- nuberObjList[4].SetActive(false);
- return;
- }
- if (Count == 8)
- {
- return;
- }
- if (Count == 7)
- {
- nuberObjList[1].SetActive(false);
- nuberObjList[3].SetActive(false);
- nuberObjList[4].SetActive(false);
- nuberObjList[6].SetActive(false);
- return;
- }
- if (Count == 6)
- {
- nuberObjList[2].SetActive(false);
- return;
- }
- if (Count == 5)
- {
- nuberObjList[2].SetActive(false);
- nuberObjList[4].SetActive(false);
- return;
- }
- if (Count == 4)
- {
- nuberObjList[0].SetActive(false);
- nuberObjList[4].SetActive(false);
- nuberObjList[6].SetActive(false);
- return;
- }
- if (Count == 3)
- {
- nuberObjList[1].SetActive(false);
- nuberObjList[4].SetActive(false);
- return;
- }
- if (Count == 2)
- {
- nuberObjList[1].SetActive(false);
- nuberObjList[5].SetActive(false);
- return;
- }
- if (Count == 1)
- {
- nuberObjList[1].SetActive(false);
- nuberObjList[0].SetActive(false);
- nuberObjList[3].SetActive(false);
- nuberObjList[4].SetActive(false);
- nuberObjList[6].SetActive(false);
- return;
- }
- if (Count == 0)
- {
- nuberObjList[3].SetActive(false);
- return;
- }
- }
-
- public void Slider(float indet)
- {
-
- SetNuber((int)indet);
- }
-
- }
再由一个总的类去管理四个子数字,然后通过子数字的转换,从而控制一组数字的 ,这里面唯一有点懵逼的地方就是数据类型转换,后来把整串数据都变成String类型,再把数据类型转变char数组,然后再摘取每个的char值去重新转回int数值,从而赋值给每个数字
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
-
- //数字计算
- public class CalculateNumber : MonoBehaviour
- {
- public NuberCtrl nb1, nb2, nb3, nb4;
-
- public int count = 1000; //每次默认加50
- // Start is called before the first frame update
- void Start()
- {
- dealCount(count);
- }
- void dealCount(int I)
- {
- if (I > 9999 || I < 0)
- {
- return;
- }
- if (I <= 9999 && I > 999)
- {
- string str = I.ToString();
- char[] c = str.ToCharArray();
- nb4.SetNuber(int.Parse(c[3].ToString()));
- nb3.SetNuber(int.Parse(c[2].ToString()));
- nb2.SetNuber(int.Parse(c[1].ToString()));
- nb1.SetNuber(int.Parse(c[0].ToString()));
- }
- if (I <= 999 && I > 99)
- {
- nb1.SetNuber(0);
- string str = I.ToString();
- char[] c = str.ToCharArray();
- nb4.SetNuber(int.Parse(c[2].ToString()));
- nb3.SetNuber(int.Parse(c[1].ToString()));
- nb2.SetNuber(int.Parse(c[0].ToString()));
- }
- if (I <= 99 && I > 9)
- {
- nb1.SetNuber(0);
- nb2.SetNuber(0);
- string str = I.ToString();
- char[] c = str.ToCharArray();
- nb4.SetNuber(int.Parse(c[1].ToString()));
- nb3.SetNuber(int.Parse(c[0].ToString()));
- }
- if (I <= 9)
- {
- nb1.SetNuber(0);
- nb2.SetNuber(0);
- nb3.SetNuber(0);
- string str = I.ToString();
- char[] c = str.ToCharArray();
- nb4.SetNuber(int.Parse(c[0].ToString()));
- }
- count = I;
- }
-
- public void ADD()
- {
- dealCount(count + 50);
- }
-
- public void Reduction()
- {
-
- dealCount(count - 50);
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。