小编Pro*_*mer的帖子

如何使用不安全的代码Unity

我想在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)

c# c++ clr unsafe unity-game-engine

6
推荐指数
2
解决办法
1万
查看次数

比较Unity中的浮动

我的场景中有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".无论如何有任何想法为什么会发生这种情况?

c# floating-point unity-game-engine

6
推荐指数
2
解决办法
4799
查看次数

执行raycast时,Camera.main为null

生成错误的代码:

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)

c# unity-game-engine unity3d-2dtools

6
推荐指数
1
解决办法
1889
查看次数

Print/Debug.Log未在Unity控制台上显示输出

我试图在Unity Console上使用C#打印一个简单的语句,但我不知道它为什么不打印. 在此输入图像描述

c# console unity-game-engine

6
推荐指数
2
解决办法
1万
查看次数

将RenderTexture转换为Texture2D

我需要将一个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)

c# io export unity-game-engine

6
推荐指数
1
解决办法
1万
查看次数

获取安卓版本信息

我想从 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)

c# android unity-game-engine

6
推荐指数
1
解决办法
3327
查看次数

读写剪贴板

我在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?有什么我不理解的吗?我应该用什么呢?

c# clipboard unity-game-engine clipboarddata

6
推荐指数
1
解决办法
2090
查看次数

在Unity中使用dynamic keyword/.NET 4.6功能

我正在尝试将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中的错误相同

c# database unity-game-engine

6
推荐指数
2
解决办法
4470
查看次数

我可以在Unity3d 2017.2中使用Java脚本语言吗?

我注意到在MonoDevelop编辑器中javascript的自动更正它不起作用.这是否意味着Unity愿意放弃JS?我可以在Unity 2017.2中使用Java脚本语言吗?

javascript unity-game-engine unityscript

6
推荐指数
1
解决办法
5257
查看次数

如何在给定的持续时间内为贝塞尔曲线设置动画

我通过将以下脚本添加到检查器中的空游戏对象来创建贝塞尔曲线.当我运行代码时,这将立即绘制完成曲线.如何在给定的时间段内制作动画,例如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)

c# unity-game-engine

6
推荐指数
1
解决办法
175
查看次数