Unity中的福彩12选5走势图故障

波兰斯基箭

新成员
已加入
2019年12月1日
留言内容
3
编程经验
Beginner
嗨,我在Unity游戏中为GUI编写代码,我首先为此制作了一层。我想在单击Tab之后测试脚本显示和隐藏福彩12选5走势图的脚本,并且福彩12选5走势图每秒显示和隐藏多次。

这是负责此功能的代码片段:
C#:
if (openinventory)
        {
            GUI.DrawTexture(new Rect(Screen.width - Screen.width / 1.2f, Screen.height - Screen.height / 1.2f, Screen.width * 0.6666f, Screen.height * 0.6666f), inventoryLabel);
            Cursor.visible = true;
            Cursor.lockState = wantedMode;
        }

这里是记录的问题:
(顺便说一句:您知道打开存货或任何其他条件后如何关闭旋转吗?)

这是整个游戏代码(如果需要):
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : MonoBehaviour
{
    private float maxHealth = 100;
    private float currentHealth = 100;
    private float maxArmour = 100;
    private float currentArmour = 100;
    private float maxStamina = 500;
    private float currentStamina = 500;
    private float maxHunger = 100;
    private float currentHunger = 100;
    private float HPbarWidth;
    private float ARMbarWidth;
    private float STbarWidth;
    private float HGbarWidth;
    private float barHeight;
    private float barShadowWidth;
    private float barShadowHeight;
    public Texture2D HPtexture;
    public Texture2D ARMtexture;
    public Texture2D STtexture;
    public Texture2D HGtexture;
    public Texture2D shadowTexture;
    public float walkSpeed = 10f;
    public float runSpeed = 20f;
    private float canHeal;
    private float canRegenerate;
    private bool hurt = false;
    private bool hrt = false;
    private bool blood = false;
    private float sec;
    private float opacity;
    private float opacity2;
    private float fullopacity = 100;
    public Texture2D bloodTexture;
    public Texture2D redScreen;
    public Texture2D iconTexture;
    public Texture2D iconFloor;
    private bool leftIcon1 = false;
    private bool leftIcon2 = true;
    private bool leftIcon3 = false;
    private bool grt2 = false;
    private bool grt3 = false;
    private bool glt2 = false;
    private bool glt1 = false;
    private bool rightIcon1 = false;
    private bool rightIcon2 = true;
    private bool rightIcon3 = false;
    private bool gt1 = false;
    private bool gt2 = false;
    private bool gt3 = false;
    private bool openinventory = false;
    public Texture2D inventoryLabel;
    CursorLockMode wantedMode;
    private Vector3 lastPosition;
    private CharacterController chCont;
    private UnityStandardAssets.Characters.FirstPerson.FirstPersonController fpsC;

    void Awake()
    {
        HPbarWidth = Screen.width - 20;
        ARMbarWidth = Screen.width / 3 - 15;
        STbarWidth = Screen.width / 3 - 15;
        HGbarWidth = Screen.width / 3 - 15;
        barHeight = HPbarWidth / 50;
        barShadowWidth = Screen.width;
        barShadowHeight = barHeight + 10;
        lastPosition = transform.position;
        chCont = GetComponent<CharacterController>();
        fpsC = gameObject.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>();
    }

    private void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 0, barShadowWidth, barShadowHeight), shadowTexture);
        GUI.DrawTexture(new Rect(0, Screen.height - barShadowHeight, barShadowWidth, barShadowHeight), shadowTexture);
        GUI.DrawTexture(new Rect(10, 5, currentHealth * HPbarWidth / maxHealth, barHeight), HPtexture);
        GUI.DrawTexture(new Rect(10, Screen.height - barHeight - 5, currentArmour * ARMbarWidth / maxArmour, barHeight), ARMtexture);
        GUI.DrawTexture(new Rect(Screen.width / 3 + 5, Screen.height - barHeight - 5, currentStamina * STbarWidth / maxStamina, barHeight), STtexture);
        GUI.DrawTexture(new Rect(Screen.width / 3 * 2 + 5, Screen.height - barHeight - 5, currentHunger * HGbarWidth / maxHunger, barHeight), HGtexture);

        if (hurt)
        {
            opacity = 1;
            blood = true;
            hrt = true;
            if (hrt)
            {
                hurt = false;
                hrt = false;
            }
        }

        if (blood && currentArmour <= 0)
        {
            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, opacity);
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), bloodTexture, ScaleMode.ScaleToFit);
            StartCoroutine("waitAndChangeOpacity");
        }

        if (opacity <= 0)
        {
            blood = false;
        }

        if (currentHealth <= 0)
        {
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), bloodTexture, ScaleMode.ScaleToFit);
            blood = false;
            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, opacity2);
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), redScreen);
            StartCoroutine("halfOpacity");
        }

        GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, fullopacity);
        GUI.DrawTexture(new Rect(100, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
        GUI.DrawTexture(new Rect(150, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
        GUI.DrawTexture(new Rect(200, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
        GUI.DrawTexture(new Rect(Screen.width - 140, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
        GUI.DrawTexture(new Rect(Screen.width - 190, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
        GUI.DrawTexture(new Rect(Screen.width - 240, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);

        if (grt2)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                leftIcon2 = true;
                leftIcon1 = false;
                grt2 = false;
            }
        }

        if (grt3)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                leftIcon3 = true;
                leftIcon2 = false;
                grt3 = false;
            }
        }

        if (glt2)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                leftIcon2 = true;
                leftIcon3 = false;
                glt2 = false;
            }
        }

        if (glt1)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                leftIcon1 = true;
                leftIcon2 = false;
                glt1 = false;
            }
        }

        if (leftIcon1)
        {
            GUI.DrawTexture(new Rect(100, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
        }

        if (leftIcon2)
        {
            GUI.DrawTexture(new Rect(150, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
        }

        if (leftIcon3)
        {
            GUI.DrawTexture(new Rect(200, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
        }

        if (gt1)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                rightIcon1 = true;
                rightIcon3 = false;
                gt1 = false;
            }
        }

        if (gt2)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                rightIcon2 = true;
                rightIcon1 = false;
                gt2 = false;
            }
        }

        if (gt3)
        {
            sec = 0.01f;
            sec -= Time.deltaTime;
            if (sec <= 0)
            {
                rightIcon3 = true;
                rightIcon2 = false;
                gt3 = false;
            }
        }

        if (rightIcon1)
        {
            GUI.DrawTexture(new Rect(Screen.width - 240, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
        }

        if (rightIcon2)
        {
            GUI.DrawTexture(new Rect(Screen.width - 190, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
        }

        if (rightIcon3)
        {
            GUI.DrawTexture(new Rect(Screen.width - 140, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
        }

        if (openinventory)
        {
            GUI.DrawTexture(new Rect(Screen.width - Screen.width / 1.2f, Screen.height - Screen.height / 1.2f, Screen.width * 0.6666f, Screen.height * 0.6666f), inventoryLabel);
            Cursor.visible = true;
            Cursor.lockState = wantedMode;
        }
    }

    private void FixedUpdate()
    {
        float speed = walkSpeed;

        if (chCont.isGrounded && Input.GetKey(KeyCode.LeftShift) && lastPosition != transform.position && currentStamina > 0)
        {
            lastPosition = transform.position;
            speed = runSpeed;
            fpsC.CanRun = true;
            currentStamina -= 1;
            currentStamina = Mathf.Clamp(currentStamina, 0, maxStamina);
            canHeal = 5;
        }
        else
        {
            fpsC.CanRun = false;
        }

        if (canHeal > 0)
        {
            canHeal -= Time.deltaTime;
            if (canHeal <= 0)
            {
                canHeal = 0;
            }
        }

        if (canHeal <= 0)
        {
            Regenerate(ref currentStamina, maxStamina);
        }

        if (canRegenerate > 0)
        {
            canRegenerate -= Time.deltaTime;
            if (canRegenerate <= 0)
            {
                canRegenerate = 0;
            }
        }

        if (canRegenerate <= 0)
        {
            Regenerate(ref currentHealth, maxHealth);
        }

        if (currentHealth <= 0)
        {
            canRegenerate = 1;
            fpsC.CanWalk = false;
            fpsC.CanJump = false;
        }

        if (currentStamina > 0)
        {
            fpsC.CanJump = true;
        }
        else if (currentStamina <= 0)
        {
            fpsC.CanJump = false;
        }

        if (currentHealth > 0)
        {
            fpsC.CanJump = true;
        }
        else if (currentHealth <= 0)
        {
            fpsC.CanJump = false;
        }
    }

    void Regenerate(ref float currentStat, float maxStat)
    {
        currentStat += 1;
        currentStat = Mathf.Clamp(currentStat, 0, maxStat);
    }

    void TakeHit(float damage)
    {
        if (currentArmour > 0)
        {
            currentArmour -= damage;
            if (currentArmour < 1)
            {
                currentHealth += currentArmour;
                currentArmour = 0;
                canRegenerate = 5;
            }
        }
        else if (currentArmour < 1)
        {
            currentHealth -= damage;
            canRegenerate = 5;
            if (currentHealth < 1)
            {
                currentHealth = 0;
            }
        }
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            TakeHit(30);
            hurt = true;
        }

        if (Input.GetKeyDown(KeyCode.E) && leftIcon1)
        {
            grt2 = true;
        }

        if (Input.GetKeyDown(KeyCode.E) && leftIcon2)
        {
            grt3 = true;
        }

        if (Input.GetKeyDown(KeyCode.Q) && leftIcon3)
        {
            glt2 = true;
        }

        if (Input.GetKeyDown(KeyCode.Q) && leftIcon2)
        {
            glt1 = true;
        }

        if (Input.GetKeyDown(KeyCode.R) && rightIcon1)
        {
            gt2 = true;
        }

        if (Input.GetKeyDown(KeyCode.R) && rightIcon2)
        {
            gt3 = true;
        }

        if (Input.GetKeyDown(KeyCode.R) && rightIcon3)
        {
            gt1 = true;
        }

        if (Input.GetKeyDown(KeyCode.Space) && currentStamina > 0 && currentHealth > 0)
        {
            currentStamina -= 50;
            currentStamina = Mathf.Clamp(currentStamina, 0, maxStamina);
            canHeal = 5;
        }

        if (Input.GetKeyDown(KeyCode.Tab) && !openinventory)
        {
            openinventory = true;
        }
        else if (Input.GetKeyDown(KeyCode.Tab) && openinventory)
        {
            openinventory = false;
        }
    }

    IEnumerator waitAndChangeOpacity()
    {
        yield return new WaitForEndOfFrame();
        opacity -= 0.005f;
    }

    IEnumerator halfOpacity()
    {
        yield return new WaitForEndOfFrame();
        opacity2 = 0.5f;
    }
}
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,933
地点
英国
编程经验
10+
我要给您的唯一建议是分开您的逻辑。

您应该做的是将所有业务,工作人员,模型,UI逻辑等分开。而且,与其将所有内容都转储为一种方法,还不如编写干净的描述性强健的模块化代码。非常模块化,特别适合Unity!将您的代码分开,然后将其分解成几个类,其中您创建的每个类仅提供特定的功能,并且没有超出其用途。
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,933
地点
英国
编程经验
10+
你知道打开存货后如何关闭旋转吗
旋转什么? Unity由您自己来为任何移动或旋转的对象编写自己的代码。如果有东西在移动,那么您需要结束负责使其移动的代码周期。 Unity中没有任何对象会自行旋转,而是由您编写的代码启动的。
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,536
地点
弗吉尼亚州切萨皮克
编程经验
10+
You have a variable named wantedMode that you assign to the cursor's lock state, but it is not clear where you set the variable to any particular value.
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,933
地点
英国
编程经验
10+
在243上,您正在设置福彩12选5走势图可见性,并且将lockstate设置为默认的有用模式是没有意义的,尤其是
锁定后,福彩12选5走势图将放置在视图的中心,并且无法移动。福彩12选5走势图是 在这种状态下不可见,无论Cursor.visible的值如何。
似乎您正在发生其他事情,这些事情可以控制福彩12选5走势图,从而导致这种闪烁。我也没有看到您在哪里使用通缉模式,但我不认为这是任何闪烁的原因。
应该像这样检查/设置: Cursor.lockState = CursorLockMode.Locked
 
最佳 底部