c# - Unity C# 可滚动 GUI.BOX 不适用于 android

标签 c# android unityscript unity3d

我有一个 GUI 我在点击商店按钮时正在绘制,有 GUI.BeginScrollView 和一些按钮,无论如何,我的 ScrollView 只在unity 编辑器,如果我在手机上运行它不起作用,我看到按钮但没有滚动条。

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class Shop : MonoBehaviour
    {
        private bool PopUp;
        public string Info;
        public Touch touch;
        public Vector2 scrollPosition = Vector2.zero;

        void Update()
        {
            if (Input.touchCount > 0)
            {
                touch = Input.touches[0];
                if (touch.phase == TouchPhase.Moved)
                {
                    scrollPosition.y += touch.deltaPosition.y;
                }
            }

        }

        void OnGUI()
        {
            Rect rect = new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2);
            Rect close = new Rect(Screen.width / 4 + Screen.width / 2, Screen.height / 4, Screen.width / 30, Screen.width / 30);
            Rect a= new Rect(0, 0, Screen.width / 2 - 2 * Screen.width / 50, Screen.height / 10);
            Rect b= new Rect(0, Screen.height / 10, Screen.width / 2 - 2 * Screen.width / 50, Screen.height / 10);
            Rect c= new Rect(0, 2 * Screen.height / 10, Screen.width / 2 - 2 * Screen.width / 50, Screen.height / 10);
            Rect d= new Rect(0, 3 * Screen.height / 10, Screen.width / 2 - 2 * Screen.width / 50, Screen.height / 10);
            Rect e= new Rect(0, 4 * Screen.height / 10, Screen.width / 2 - 2 * Screen.width / 50, Screen.height / 10);
            Rect f= new Rect(0, 5 * Screen.height / 10, Screen.width / 2 - 2 * Screen.width / 50, Screen.height / 10);
            if (PopUp)
            {
                GUIStyle myStyle = new GUIStyle(GUI.skin.button);
                float sizesc = Mathf.Sqrt(Mathf.Pow(Screen.width, 2) + Mathf.Pow(Screen.height, 2));
                myStyle.fontSize = (int)(0.015f * sizesc);
                scrollPosition = GUI.BeginScrollView(rect, scrollPosition, new Rect(0, 0, 0, PlayerPrefs.GetInt("unlocked") * Screen.height/10f + 10));
                if (GUI.Button(a, "Buy a", myStyle))
                {
//do something                 
                }
                if (GUI.Button(b, "Buy b", myStyle))
                {
//do something
                }
                if (GUI.Button(c, "Buy c", myStyle))
                {
//do something
                }
                if (GUI.Button(d, "Buy d", myStyle))
                {
//do something
                }
                if (PlayerPrefs.GetInt("e") > 0)
                {
                    if (GUI.Button(e, "Buy e", myStyle))
                    {
//do something
                    }
                }
                if (PlayerPrefs.GetInt("f") > 0)
                {
                    if (GUI.Button(f, "Buy f", myStyle))
                    {
//do something
                    }
                }
                GUI.EndScrollView();
                if (GUI.Button(close, "X"))
                {
                    PopUp = false;
                }
            }
        }

        public void onClick()
        {
            PopUp = true;
        }
    }

我不明白为什么它只适用于 UnityEditor 而不适用于我的 android 设备。

最佳答案

解决方案是改变这一行:

  scrollPosition = GUI.BeginScrollView(rect, scrollPosition, new Rect(0, 0, 0, PlayerPrefs.GetInt("unlocked") * Screen.height/10f));

表示按钮个数*按钮高度,为:

  scrollPosition = GUI.BeginScrollView(rect, scrollPosition, new Rect(0, 0, 0, PlayerPrefs.GetInt("unlocked") * Screen.height/10f + 10));

只是使 Gui.BeginScrollView 按钮的长度变大一点。

关于c# - Unity C# 可滚动 GUI.BOX 不适用于 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948798/

相关文章:

C# 8 使用实用方法验证参数

java - Android java模块 "jar"文件依赖问题

java - 如何判断标记是否在多边形中 googleMap v2

regex - 这是正则表达式吗?

javascript - 在语音气泡Unity 2D中渲染文本

c# - 将数组从 C# (Mono) 编码到 C++

c# - 如何在不使用 IDE 的情况下更改 C# 程序的图标?

c# - 将数字附加到名称(字符串)

c# - 接口(interface)上的扩展方法

javascript - 如何使用phonegap和javascript播放本地媒体文件?