您当前的位置: 首页 >  unity

幻世界

暂无认证

  • 0浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Unity开发小技巧】Unity随机概率扩展(概率可调控)

幻世界 发布时间:2019-01-09 11:43:19 ,浏览量:0

做了以下两张图有助于理解,如果想调控概率的话直接修改概率数组即可,实战案例:http://t.csdn.cn/P9QKJ

其实在做概率类相关的界面效果的时候,我们真实做法都是在刷新界面前已经把结果获取到了,然后根据结果去处理界面上的逻辑,一定要带着这个思想去理解以下内容 

一.做加法
   /**加*/
    //rate:几率数组(%),  total:几率总和(100%)
   // Debug.Log(rand(new int[] { 10, 5, 15, 20, 30, 5, 5,10 }, 100));
    public static int rand(int[] rate, int total)
    {
        int r = Random.Range(1, total+1);
        int t = 0;
        for (int i = 0; i < rate.Length; i++)
        {
            t += rate[i];
            if (r < t)
            {
                return i;
            }
        }
        return 0;
    }
二.做减法
  /**减*/
    //rate:几率数组(%),  total:几率总和(100%)
    // Debug.Log(randRate(new int[] { 10, 5, 15, 20, 30, 5, 5,10 }, 100));
    public static int randRate(int[] rate, int total)
    {
        int rand = Random.Range(0, total+1);
        for (int i = 0; i < rate.Length; i++)
        {
            rand -= rate[i];
            if (rand             
关注
打赏
1660704426
查看更多评论
0.0396s