2016-04-29 195 views
0

我最近在使用Unity中的#region,#endregion,#if,#endif等類似的東西時遇到了問題。Unity預處理器指令錯誤?

我不記得剛開始的Unity版本是什麼,但是每當我創建一個新項目時,我根本無法使用區域。

它總是說,有一個解析錯誤,然後說這樣的事情「錯誤CS1027:預計`#ENDIF」指令

爲了得到這個錯誤,這是所有我把

#if !UNITY_EDITOR 
#endif 

,如果我有語句之間的代碼,或剝離周圍的指令兩邊都是空的空間沒關係..

我有另外一個,舊的項目,我可以使用#regions和#如果陳述在f不知道發生了什麼變化或如何解決它..我一直在搜索解決方案,似乎沒有人會遇到這個問題?這是一個monodevelop的設置嗎?白色空間?無效字符的地方?我真的不知道爲什麼會發生這種情況,這讓我很生氣,哈哈。

如果有人有任何建議,我很樂意聽到他們!

謝謝你的時間!

編輯: 下面是一個#地區不適用於我的拖放腳本..(獎金,自由拖放腳本!大聲笑)也許只是註釋掉地區,如果他們給你錯誤。 。 我不得不。 :(

統一控制檯錯誤:

資產/腳本/ DragAndDrop.cs(12254):錯誤CS1028:意外處理器指令(沒有#地區此#endregion)(這一個指向我的結束摘要標籤) 資產/腳本/ DragAndDrop.cs(14,45):錯誤CS1028:意外的處理器指令(無#地區本#endregion) ...等

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 
} 
+0

如果當前VS配置爲「釋放」,則將其切換爲「調試」。 – raven

+0

嘿!謝謝回覆。我正在使用Monodevelop,它目前設置爲調試!感謝這個想法。 – JoeyMaru

+0

嘿喬!是的,在控制檯中,是的,我正在努力獲得一個例子。你發佈的答案對我來說工作得很好,即使在現在的新項目中也是如此。但奇怪的是,我已經使用了這些指令很多,很多,我喜歡區域組織我的代碼。在之前的項目中,他們根本就不工作,但是我做了一個新項目在Unity 5.3.2f1中測試,現在區域似乎工作正常了?我現在抓住幾個例子。 – JoeyMaru

回答

3

所以,對實施例YOU GIVE。

I將其粘貼到一個普通的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 
+0

好吧,我打算髮布一些pastebin腳本。這是我從一位正在致力於將facebook整合到獨立團結構建中的紳士手中獲得的腳本。這是他的一個腳本http://pastebin.com/HN43ZjEM。我對錯誤說明的位置以及實際位置發表了一些評論。 Unity有時會遇到問題,我也指出了正確的路線。 – JoeyMaru

+0

好的!謝謝,我會那樣做的!我需要做一個快速的差事,我會在大約15分鐘後回來,我打開了我的遊戲擁堵項目,在那裏我也遇到了問題,我會提供更多的例子。再次感謝您的時間,我非常感謝所有的幫助!我想確定這個問題是什麼。 – JoeyMaru

+0

你很可能是對的。不知道還有什麼可以的。我發佈了一個拖放腳本,在我的問題中給了我#region錯誤。真正奇怪的是 - 與地區相同的代碼在另一個項目中完美地工作。但是當我把這些代碼放進一個新的統一項目時,我發現了所有這些錯誤。我很迷茫。 :( – JoeyMaru