簡體   English   中英

Unity預處理程序指令錯誤?

[英]Unity Preprocessor Directive Error?

我最近在Unity中使用#region,#endregion,#if,#endif等問題時遇到問題。

我不記得它最初是從哪個版本開始的,但是每當我創建一個新項目時,我根本就不會使用區域。

它總是說存在解析錯誤,然后說類似“ 錯誤CS1027:預期的#endif指令

為了得到這個錯誤,這就是我要做的

#if !UNITY_EDITOR
#endif

我是否在語句之間包含代碼,還是在指令的兩側刪除所有空白都沒有關系。

我有另一個較舊的項目,可以很好地使用#regions和#if語句,不確定是否更改了內容或如何修復它。.我一直在尋找解決方案,似乎沒人這個問題? 這是Monodevelop中的設置嗎? 空格? 某個地方的字符無效? 我真的不知道為什么會這樣,這讓我發瘋,哈哈。

如果有人有任何建議,我很想聽聽他們的建議!

謝謝你的時間!

編輯:這是#regions在拖放腳本中不為我工作的示例。.(獎勵,免費拖放和腳本!大聲笑)如果它們給您錯誤,也許只是注釋掉它們。 :(

Unity控制台錯誤:

Assets / Scripts / DragAndDrop.cs(12,254):錯誤CS1028:意外的處理器指令(此#endregion沒有#region)(這使我指向了摘要標記的結尾?)Assets / Scripts / DragAndDrop.cs(14, 45):錯誤CS1028:意外的處理器指令(此#endregion沒有#region)...等等

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// DragAndDrop.
/// This class will be responsible for listening to drag
/// events on the gameobject.
/// It will handle what happens during each drag state.
/// </summary>

public class DragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler{
public RectTransform canvas;        // the uGui canvas in your scene

#region DRAG BOOLEANS
public bool canDrag = true;         // can this object be dragged?
public bool wasDragged = false;     // was this object recently dragged?
public bool isDragging = false;     // is object currently being dragged
public bool dragOnSurfaces = true;  
#endregion

#region SWIPE
public float comfortZoneVerticalSwipe = 50;     // the vertical swipe will have to be inside a 50 pixels horizontal boundary
public float comfortZoneHorizontalSwipe = 50;   // the horizontal swipe will have to be inside a 50 pixels vertical boundary
public float minSwipeDistance = 14;             // the swipe distance will have to be longer than this for it to be considered a swipe

public float startTime;     // when the touch started
public Vector2 startPos;    // where the touch started
public float maxSwipeTime;  // if the touch lasts longer than this, we consider it not a swipe
#endregion

#region PRIVATE
private GameObject draggingObject;
private RectTransform draggingTransform;
#endregion

#region UNITY CALLBACKS
void Awake(){
    canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
}
#endregion

#region TOUCH EVENTS
public void OnPointerDown(PointerEventData eventData){
    if(canDrag){
        wasDragged = false;
        // make sure object is parent to the canvas or it will disappear when picked up
        // I had to do this in word addiction because the letters were parented to tiles
        gameObject.transform.SetParent(canvas);
        // scale up when touched
        gameObject.transform.localScale = new Vector3(2, 2, 2);
    }
}

public void OnPointerUp(PointerEventData eventData){
    if(canDrag){
        // scale back down
        gameObject.transform.localScale = new Vector3(1, 1, 1);
    }
}
#endregion

#region DRAG EVENTS
public void OnBeginDrag(PointerEventData eventData){    
    if(canDrag){
        // start listening for swipe
        startPos = eventData.position;
        startTime = Time.time;

        // set drag variables
        isDragging = true;
        wasDragged = true;

        // run pick up logic
        PickUp(eventData);
    }
}

public void OnDrag(PointerEventData eventData){
    if(canDrag){
        if(draggingObject != null){
            Move(eventData);
        }
    }
}

public void OnEndDrag(PointerEventData eventData){
    if(canDrag){
        // swipe detection
        bool shouldFlick = false;
        float swipeTime = Time.time - startTime;
        float swipeDist = (eventData.position - startPos).magnitude;
        if (swipeTime < maxSwipeTime &&
            swipeDist > minSwipeDistance){
            shouldFlick = true;
        }
        // handle swipe/dropping
        if (shouldFlick){
            Debug.Log("FLICK");
        }else{
            isDragging = false;
            Place();
        }
    }
}
#endregion

#region EVENT FUNCTIONS
void PickUp(PointerEventData eventData){
    draggingObject = gameObject;
    Move(eventData);
}

void Move(PointerEventData eventData){
    if(dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null){
        draggingTransform = eventData.pointerEnter.transform as RectTransform;
    }

    var rt = draggingObject.GetComponent<RectTransform>();
    Vector3 globalMousePos;
    if(RectTransformUtility.ScreenPointToWorldPointInRectangle(draggingTransform, eventData.position, eventData.pressEventCamera, out globalMousePos)){
        rt.position = globalMousePos;
        rt.rotation = draggingTransform.rotation;
    }
}

void Place(){
    Vector2 pos = new Vector2(transform.position.x, transform.position.y);
    Collider2D[] cols = Physics2D.OverlapCircleAll(pos, 10);
    float closestDistance = 0;
    GameObject closest = null;
    foreach(Collider2D col in cols){
        if(col.tag == "SomeTagToCheckFor"){
            Vector2 otherPos = new Vector2(col.transform.position.x, col.transform.position.y);
            if(closest == null){
                closest = col.gameObject;
                closestDistance = Vector2.Distance(pos, otherPos);
            } else{
                // here we will check to see if any other objects
                // are closer than the current closest object
                float distance = Vector2.Distance(pos, otherPos);
                if(distance < closestDistance){
                    // this object is closer
                    closest = col.gameObject;
                    closestDistance = distance;
                }
            }
        }
    }

    // snap to the closest object
    if(closest != null){
        // if something was detected to snap too?
    } else{
        // return object back?
    }
}
#endregion
}

因此,關於您給出的示例。

我將其粘貼到普通Unity5項目中的文件HybFacebookExtensions.cs中。 它運行完美-沒有錯誤。

您的Unity安裝可能存在問題。

不幸的是,沒有人能夠猜測那里出了什么問題。 您還有第二台機器要測試嗎?


#if !UNITY_EDITOR
Debug.Log("YO");
#endif

是一個正確的例子。

您可能不小心將其從Debug更改為Release。

注意。 您會非常困惑地得到這些錯誤,

如果您的代碼只是簡單的語法錯誤。

真煩人。 以下示例可能會導致此類異常錯誤:

public Class Teste()
{
.. you meant to put it in here ..
}
#if UNITY_EDITOR
#endif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM