我想在c#中使用c ++代码来使用CLR进行统一.
该程序在统一之外正常工作,但在引擎内部它给我一个错误:
"cs0227:不安全的代码需要指定'unsafe'命令行选项"
我真的很困惑,因为该项目在visual studio中成功构建(没有任何错误或警告).我已激活" 允许 不安全 "按钮.
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class newspawn_real : MonoBehaviour {
void Start () {
unsafe
{
fixed (int * p = &bam[0, 0, 0])
{
CppWrapper.CppWrapperClass controlCpp = new CppWrapper.CppWrapperClass();
controlCpp.allocate_both();
controlCpp.fill_both();
controlCpp.fill_wrapper();
}
Run Code Online (Sandbox Code Playgroud) 我的场景中有6个InputFields.他们的内容类型是十进制.
我从这些输入字段中获取值并检查它们的总和是否等于100.02.我在所有人中输入16.67.
float fireP = float.Parse(firePercentage.text);
float waterP = float.Parse(waterPercentage.text);
float lightP = float.Parse(lightPercentage.text);
float nightP = float.Parse(nightPercentage.text);
float natureP = float.Parse(naturePercentage.text);
float healthP = float.Parse(healthPercentage.text);
float total = fireP + waterP + lightP + nightP + natureP + healthP;
if (total == 100.02f)
{
Debug.Log("It's equal");
}
else
{
Debug.Log(" Not equal. Your sum is = " + total);
}
Run Code Online (Sandbox Code Playgroud)
我在控制台日志中得到"不等于.你的总和= 100.02".无论如何有任何想法为什么会发生这种情况?
生成错误的代码:
void Update()
{
if (Input.touchCount > 0)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
if (hit && hit.collider != null && hit.collider.name == "leftTapArea")
{
hit.transform.name = "Hit";
}
}
}
Run Code Online (Sandbox Code Playgroud)
它说这个字符串有问题:
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position),Vector2.zero);
错误:
NullReferenceException:对象引用未设置为对象的实例leftScript.Update()(在Assets/leftScript.cs:16)
我需要将一个RenderTexture对象保存为.png文件,然后将该文件用作包裹3D对象的纹理.我的问题是现在我无法使用EncodeToPNG()保存RenderTexture对象,因为RenderTexture不包含该方法.如何将RenderTexture对象转换为Texture2D对象?谢谢!
// Saves texture as PNG file.
using UnityEngine;
using System.Collections;
using System.IO;
public class SaveTexture : MonoBehaviour {
public RenderTexture tex;
// Save Texture as PNG
void SaveTexturePNG()
{
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
}
}
Run Code Online (Sandbox Code Playgroud) 我想从 Unity 获取 Android 版本信息。我知道SystemInfo.operatingSystem可以用来执行此操作,但它没有所有信息,例如版本号、代号和其他信息。
我决定AndroidJavaClass使用 Android 的Build.VERSION类制作一个带有 Unity类的小插件,但遇到了一个我无法解释的问题。
当我做:
AndroidJavaClass("android.os.Build.VERSION");
Run Code Online (Sandbox Code Playgroud)
我得到类未找到异常。
它在使用时起作用:
AndroidJavaClass("android.os.Build$VERSION");
Run Code Online (Sandbox Code Playgroud)
请注意,我替换了“。” 使用“$”,现在可以找到该类。
我过去写过很多插件,以前从未遇到过这个问题。对于例如,当我访问Android的Uri课,我使用 AndroidJavaClass("android.net.Uri");和它的工作。我不必将“$”放在“”之前Uri。
是什么让访问android.net.Uri与访问不同android.os.Build.VERSION?
为什么必须在 Build 和 VERSION 之间加上“$”AndroidJavaClass才能找到这个类?
顺便说一下,这是Build.VERSIONUnity 中的工作插件:
public class AndroidVersion
{
static AndroidJavaClass versionInfo;
static AndroidVersion()
{
versionInfo = new AndroidJavaClass("android.os.Build$VERSION");
}
public static string BASE_OS
{
get
{
return …Run Code Online (Sandbox Code Playgroud) 我在Unity 5.6上的Windows(VS2017社区)上有这个片段:
public static void setClipboardStr(string str)
{
try
{
if (Clipboard.ContainsText())
{
// ...doesn't matter if true or false -
// from here on, I can't copy+paste inside
// the game or outside until I close the app.
// If I had an error instead of try/catch or
// check if it contains text, the error will
// remain UNTIL I REBOOT (beyond the app closing).
}
}
catch(Exception ex)
{
Debug.LogError(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
每当我以任何形式使用剪贴板时,即使检查它是否是文本,它都会破坏剪贴板,直到我关闭应用程序.现在,这是一个Unity错误吗?VS bug?有什么我不理解的吗?我应该用什么呢?
我正在尝试将GraphQL实现到Unity3D(版本2017.1.0f3 Personal).我使用的是.NET 4.6(实验版),但尽管如此,Unity还是不支持动态关键字.这很奇怪,因为.NET 4.0是.NET的一部分.在Unity中除外.我正在谷歌搜索一些解决方案如何让它工作,但没有动态关键字的解决方案.错误是这样的:
Severity Code Description Project File Line Suppression State
Error CS1980 Cannot define a class or member that utilizes 'dynamic'
because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you
missing a reference? skiing-prototype (1) D:\skiing-prototype
(1)\Assets\Scripts\GraphQL.cs 62 Active
Run Code Online (Sandbox Code Playgroud)
这是使用GraphQL C#客户端的唯一警告.还有人试过它才能让它发挥作用吗?我还没有找到任何更大的努力来让它运行起来.
编辑:
我在这里使用这个客户端:https://github.com/bkniffler/graphql-net-client
这也是visual studio的一个错误,但在Unity控制台中它也会显示错误,会暂时更新
Assets/Scripts/GraphQL.cs(80,16): error CS1980: Dynamic keyword requires
`System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you
missing System.Core.dll assembly reference?
Run Code Online (Sandbox Code Playgroud)
这是统一编辑器错误,这似乎与visual studio中的错误相同
我注意到在MonoDevelop编辑器中javascript的自动更正它不起作用.这是否意味着Unity愿意放弃JS?我可以在Unity 2017.2中使用Java脚本语言吗?
我通过将以下脚本添加到检查器中的空游戏对象来创建贝塞尔曲线.当我运行代码时,这将立即绘制完成曲线.如何在给定的时间段内制作动画,例如2或3秒?
public class BCurve : MonoBehaviour {
LineRenderer lineRenderer;
public Vector3 point0, point1, point2;
int numPoints = 50;
Vector3[] positions = new Vector3[50];
// Use this for initialization
void Start () {
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material (Shader.Find ("Sprites/Default"));
lineRenderer.startColor = lineRenderer.endColor = Color.white;
lineRenderer.startWidth = lineRenderer.endWidth = 0.1f;
lineRenderer.positionCount = numPoints;
DrawQuadraticCurve ();
}
void DrawQuadraticCurve () {
for (int i = 1; i < numPoints + 1; i++) {
float t = i / (float)numPoints;
positions …Run Code Online (Sandbox Code Playgroud)