演示
实现
画点素材:
整一个 Unity2D 项目:
定义一个 Die
(骰子)和 GameController
类,分别绑定在 Dice
(预制体)和 Main Camera
上。写点代码:
GameController.cs Die.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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 using System;using System.Collections;using System.Collections.Generic;using System.Linq;using UnityEngine;using UnityEngine.UI;using Random = UnityEngine.Random;public class GameController : MonoBehaviour { public Text prompt; public Button button; private short [] diceRolls = new short [6 ]; public Die[] dice = new Die[6 ]; private int diceRolledCount = 0 ; void Start () { button.onClick.AddListener(Roll); foreach (var d in dice) { d.OnDiceRolled += OnDiceRolled; } } private void Update () { } public void Roll () { button.interactable = false ; prompt.text = "请稍后..." ; for (int i = 0 ; i < 6 ; i++) { diceRolls[i] = (short )Random.Range(1 , 7 ); dice[i].score = diceRolls[i]; } } private void OnDiceRolled () { diceRolledCount++; if (diceRolledCount == 6 ) { diceRolledCount = 0 ; button.interactable = true ; prompt.text = GetBoBingResult(); } } private string GetBoBingResult () { var grouped = diceRolls.GroupBy(d => d).ToList(); var sortedDice = diceRolls.OrderBy(d => d).ToArray(); if (diceRolls.All(d => d == 4 )) { return "六抔红" ; } if (grouped.Count == 1 && grouped[0 ].Key != 4 ) { return "六抔黑" ; } if (grouped.Count == 2 && grouped.Any(g => g.Count() == 4 && g.Key == 4 ) && grouped.Any(g => g.Count() == 2 && g.Key == 1 )) { return "状元插金花!" ; } if (grouped.Any(g => g.Count() == 5 )) { return "五子登科!" ; } if (grouped.Count == 2 && grouped.Any(g => g.Count() == 4 && g.Key == 4 )) { return "状元!" ; } if (sortedDice.SequenceEqual(new short [] { 1 , 2 , 3 , 4 , 5 , 6 })) { return "对堂!" ; } if (grouped.Count == 2 && grouped.Any(g => g.Count() == 4 && g.Key != 4 )) { return "四进!" ; } if (grouped.Any(g => g.Count() == 3 && g.Key == 4 )) { return "三红!" ; } if (grouped.Any(g => g.Count() == 2 && g.Key == 4 )) { return "二举!" ; } if (grouped.Any(g => g.Count() == 1 && g.Key == 4 )) { return "一秀!" ; } return "再接再厉!" ; } }
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 using System;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using Random = UnityEngine.Random;public class Die : MonoBehaviour { private short _score; public event Action OnDiceRolled; public short score { get { return _score; } set { StartCoroutine(Animation(value , () => { _score = value ; OnDiceRolled?.Invoke(); })); } } public Sprite[] diceTexture; private Image image; void Start () { image = GetComponent<Image>(); image.sprite = diceTexture[Random.Range(0 , 6 )]; } IEnumerator Animation (short value , System.Action onComplete ) { for (int i = 0 ; i < 5 ; i++) { yield return new WaitForSeconds (Random.Range(0.1f , 0.4f )) ; image.sprite = diceTexture[Random.Range(0 , 6 )]; } yield return new WaitForSeconds (Random.Range(0.1f , 0.4f )) ; image.sprite = diceTexture[value - 1 ]; onComplete?.Invoke(); } }
Unity 默认使用了 Brotli 压缩方式,这在非 https 协议下将无效,为了便于调试,将其设为 Disabled
(这会使原本 5M 的文件变成了 20M)。
使用 WebGL 打包(如果没有这个编译器,得安装一个)。