2016-04-29 191 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