Unity 预处理器指令错误?
Unity Preprocessor Directive Error?
我最近在 Unity 中使用#region、#endregion、#if、#endif 等东西时遇到问题。
我不记得这是从哪个版本的 Unity 开始的,但是每当我创建一个新项目时,我根本无法使用区域。
它总是说有一个解析错误,然后说这样的话“error CS1027: Expected `#endif' directive”
为了得到那个错误,这就是我输入的全部内容
#if !UNITY_EDITOR
#endif
无论我是在语句之间包含代码,还是去掉指令两边的所有空 space..
我有另一个较旧的项目,我可以很好地使用#regions 和#if 语句,但不确定发生了什么变化或如何修复它。我一直在谷歌搜索解决方案,看起来没有其他人有这个问题吗?是monodelop里的设置吗?白space?某个地方的字符无效?我真的不知道为什么会这样,这让我很生气,哈哈。
如果有人有任何建议,我很乐意听取他们的意见!
感谢您的宝贵时间!
编辑:
这是#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
我最近在 Unity 中使用#region、#endregion、#if、#endif 等东西时遇到问题。
我不记得这是从哪个版本的 Unity 开始的,但是每当我创建一个新项目时,我根本无法使用区域。
它总是说有一个解析错误,然后说这样的话“error CS1027: Expected `#endif' directive”
为了得到那个错误,这就是我输入的全部内容
#if !UNITY_EDITOR
#endif
无论我是在语句之间包含代码,还是去掉指令两边的所有空 space..
我有另一个较旧的项目,我可以很好地使用#regions 和#if 语句,但不确定发生了什么变化或如何修复它。我一直在谷歌搜索解决方案,看起来没有其他人有这个问题吗?是monodelop里的设置吗?白space?某个地方的字符无效?我真的不知道为什么会这样,这让我很生气,哈哈。
如果有人有任何建议,我很乐意听取他们的意见!
感谢您的宝贵时间!
编辑: 这是#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