You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2D跨平台代码报错求助:PC端出现CS1513语法错误

修复Unity 2D跨平台脚本编译错误(CS1513: } expected)

错误原因分析

你的脚本编译错误源于预处理指令区块内的括号不匹配和代码结构缩进错误,具体问题如下:

  • UNITY_STANDALONE区块中,Input.GetMouseButtonUp(0)的if语句里,嵌套的右键重启逻辑未正确包含在外层if范围内,导致括号未闭合。
  • UNITY_ANDROID区块中,Update()方法的闭合}被错误放在平台代码块内部,破坏了整个类的结构。
  • 预处理指令#endif位置未正确包裹平台相关代码,导致类的闭合逻辑混乱。

修复后的完整代码

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class HingeMovement : MonoBehaviour
{
    private bool _grabbing;
    public LineRenderer lr;
    public GameObject button;

    GameObject FindNearest()
    {
        GameObject[] hinges;
        hinges = GameObject.FindGameObjectsWithTag("hinge");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;

        foreach (GameObject go in hinges)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance; 
            }
        }
        return closest; 
    }

    void Update ()
    {
        lr.startWidth = 0.08f;
        lr.endWidth = 0.08f;
        lr.startColor = Color.yellow;
        lr.endColor = Color.yellow;

#if UNITY_STANDALONE
        button.SetActive(false);
        if (Input.GetMouseButtonDown(0))
        {
            _grabbing = true; 
        }

        if (Input.GetMouseButton(0))
        {
            lr.positionCount = 2;
            GameObject closest = FindNearest();

            if (_grabbing)
            {
                lr.SetPosition(1, closest.transform.position);
                closest.GetComponentInChildren<HingeJoint2D>().connectedBody =
                    gameObject.GetComponentInChildren<Rigidbody2D>();
                _grabbing = false; 
            }
            lr.SetPosition(0, transform.position);
        }

        if (Input.GetMouseButtonUp(0))
        {
            GameObject[] hinges;
            hinges = GameObject.FindGameObjectsWithTag("hinge");

            foreach (GameObject go in hinges)
            {
                lr.positionCount = 0;
                go.GetComponentInChildren<HingeJoint2D>().connectedBody = null; 
            }
        }

        if (Input.GetMouseButton(1)) //右键重启
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

#elif UNITY_ANDROID
        button.SetActive(true);
        if (Input.touchCount > 0)
        {
            _grabbing = true; 
        }

        if (Input.touchCount == 1)
        {
            lr.positionCount = 2;
            GameObject closest = FindNearest();

            if (_grabbing)
            {
                lr.SetPosition(1, closest.transform.position);
                closest.GetComponentInChildren<HingeJoint2D>().connectedBody =
                    gameObject.GetComponentInChildren<Rigidbody2D>();
                _grabbing = false; 
            }
            lr.SetPosition(0, transform.position);
        }

        if (Input.touchCount == 0)
        {
            GameObject[] hinges;
            hinges = GameObject.FindGameObjectsWithTag("hinge");

            foreach (GameObject go in hinges)
            {
                lr.positionCount = 0;
                go.GetComponentInChildren<HingeJoint2D>().connectedBody = null; 
            }
        }
#endif
    }

    public void PhoneRestart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

关键修复点说明

  1. 修正PC平台括号结构:将右键重启逻辑从Input.GetMouseButtonUp(0)的if块中移出,作为独立判断,确保每个if语句括号正确闭合。
  2. 调整Android平台代码结构:把Update()方法的闭合}移到#endif之后,保证Update()完整包裹所有平台逻辑代码。
  3. 规范预处理指令范围:让#if、#elif、#endif严格包裹对应平台代码块,确保类结构完整。
  4. 优化冗余代码:将SceneManager.GetActiveScene().buildIndex + 0简化为SceneManager.GetActiveScene().buildIndex,功能一致且更简洁。

内容的提问来源于stack exchange,提问作者Алексей

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 20:21:05