小编Pro*_*mer的帖子

WWW/UnityWebRequest POST/GET请求不会从server/url返回最新数据

我正在使用Unity创建一个HoloLens应用程序,它必须从REST API获取数据并显示它.我目前正在使用WWW数据类型来获取将从Update()函数调用的协程中的数据和yield return语句.当我尝试运行代码时,我从API获取最新数据,但当有人将任何新数据推送到API时,它不会实时自动获取最新数据,我必须重新启动应用程序以查看最新数据.我的代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;

public class TextChange : MonoBehaviour {

    // Use this for initialization
    WWW get;
    public static string getreq;
    Text text;
    bool continueRequest = false;

    void Start()
    {
        StartCoroutine(WaitForRequest());
        text = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private IEnumerator WaitForRequest()
    {
        if (continueRequest)
            yield break;

        continueRequest = true;

        float requestFrequencyInSec = 5f; //Update after every 5 …
Run Code Online (Sandbox Code Playgroud)

c# yield get unity-game-engine hololens

5
推荐指数
1
解决办法
3879
查看次数

Unity 5:如何从检查器传递按钮单击功能的多个参数?

我想从检查员点击按钮发送多个参数?

Unity不允许这样做,我怎样才能做到这一点?还有其他方法吗?有几十个按钮,我使用相同的方法为所有按钮,但发送不同的参数,现在我需要发送多种类型的参数.想不通怎么样?

c# parameter-passing uibutton unity5

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

如何在统一编辑器中使用Input.Location和模拟位置

我正在开发一款基于位置的游戏,有点像口袋妖怪游戏。我正在阅读Android手机上的位置,没有任何问题,但是在统一编辑器中进行开发时,我无法获取任何位置数据,因为Input.location.isEnabledByUser在编辑器中为false

可以模拟/硬编码位置,这样我就可以在不部署到手机的情况下进行尝试了。

我试图这样硬编码:

LocationInfo ReadLocation(){
    #if UNITY_EDITOR

    var location = new LocationInfo();
    location.latitude = 59.000f;
    location.longitude = 18.000f;
    location.altitude= 0.0f;
    location.horizontalAccuracy = 5.0f;
    location.verticalAccuracy = 5.0f;

    return location;
    #elif
    return Input.location.lastData;
    #endif
}
Run Code Online (Sandbox Code Playgroud)

但是该位置的所有属性都是只读的,因此出现编译错误。是否可以在编辑器中启用位置服务或对位置进行硬编码?

c# unity-game-engine

5
推荐指数
1
解决办法
4847
查看次数

处理来自 C# 的 Java 插件异常?

例如

安卓专区

public class MainActivity extends UnityPlayerActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  public int divide(int a, int b) throw Exception{
    return a / b;
  }
}
Run Code Online (Sandbox Code Playgroud)

Unity3D部分:

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
int rlt = jo.Call<int>("divide", new object[](10, 0));
Run Code Online (Sandbox Code Playgroud)

显然,在Java中调用divide(10, 0)时会产生异常。但是,Unity3D代码中可以捕获异常吗?谢谢!

c# java-native-interface android unity-game-engine

5
推荐指数
1
解决办法
1554
查看次数

在非MonoBehaviour类中使用coroutine

你怎么能在一个非Monobehaviour阶级的实例中传递Monobehaviour?我找到了这个链接,TonyLi提到你可以通过一个Monobehaviour来启动和停止一个类的实例中的协同程序,但他没有说明你怎么能这样做.他做了这个theEvent.StartEvent(myMonoBehaviour); 但是他并没有表明他从哪里获得了我的神经行为.我在互联网上环顾四周,但我似乎无法找到.

  • 编辑

这是我想要做的.我想在一个类的实例中运行一个协同程序.我还希望能够在类的实例中停止协程.我想这样做,以便我的场景中没有任何具有大型管理器的对象,并且我可以将代码重用于我想以这种方式打乒乓的任何对象.代码在一个方向上移动一个Gameobject然后休息一下并将其向另一个方向移动并再次休息等等.但我不能从课外开始协同程序.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[RequireComponent (typeof(Image))]
public class SpecialBar : MonoBehaviour {

    public float rangeX;
    public float breakTime;
    public float step;
    float startProgress = 0.5f;
    PingPongGameObject pingPonger;

    Color[] teamColors = new Color[]{new Color(255,136,0),new Color(0,170,255)};

    void Start()
    {

        for(int i = 0; i < teamColors.Length; ++i)
        {
            teamColors[i] = StaticFunctions.NormalizeColor (teamColors[i]);
        }

        pingPonger = new PingPongGameObject (gameObject.transform.position,
            new Vector3(rangeX,0.0f,0.0f),
            gameObject,
            startProgress,
            breakTime,
            step
            );
    }
}
Run Code Online (Sandbox Code Playgroud)

第二堂课是我的协程所在的地方.

public class PingPongGameObject
{
    float step; …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

5
推荐指数
1
解决办法
5023
查看次数

播放并等待动画/动画师完成播放

在我的脚本中,当玩家位于平台顶部时,我就这样做了.它工作正常.但是现在我想让它一旦起来播放剪辑"Down".

using UnityEngine;
using System.Collections;
using System.Reflection;

public class DetectPlayer : MonoBehaviour {

    GameObject target;

    public void ClearLog()
    {
        var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
        var type = assembly.GetType("UnityEditorInternal.LogEntries");
        var method = type.GetMethod("Clear");
        method.Invoke(new object(), null);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Platform")
        {
            Debug.Log("Touching Platform");
        }

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "OnTop Detector")
        {
            Debug.Log("On Top of Platform");
            GameObject findGo = GameObject.Find("ThirdPersonController");
            GameObject findGo1 = GameObject.Find("Elevator");
            findGo.transform.parent = findGo1.transform;
            target = GameObject.Find("Elevator");
            target.GetComponent<Animation>().Play("Up");
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

5
推荐指数
1
解决办法
5838
查看次数

Unity:将 Sprite 保存/加载为 Json

我正在遵循一个教程,其中我需要保存属于某个对象的精灵。我正在创建一个项目数据库,当然希望该项目附带正确的图像。这就是我构建项目数据库的方式:

项目对象

[System.Serializable]
public class ItemObject{

    public int id;
    public string title;
    public int value;
    public string toolTip;
    public bool stackable;
    public string category;
    public string slug;
    public Sprite sprite;


    public ItemObject(int id, string title, int value, string toolTip, bool stackable, string category, string slug)
    {
        this.id = id;
        this.title = title;
        this.value = value;
        this.toolTip = toolTip;
        this.stackable = stackable;
        this.category = category;
        this.slug = slug;
        sprite = Resources.Load<Sprite>("items/" + slug);
    }
Run Code Online (Sandbox Code Playgroud)

项目数据

[System.Serializable]
public class ItemData {

    public …
Run Code Online (Sandbox Code Playgroud)

c# json unity-game-engine

5
推荐指数
1
解决办法
9889
查看次数

将游戏对象移动/转移到另一个场景

我们尝试了不同的方法将 UI 对象移动到另一个场景,但我们失败了。对象在画布中。

方法 1:我们使用了 LoadLevelAdditive,但是从第一个场景中移动了所有对象,而没有通过 Canvas 及其元素。

方法 2:我们使用 DontDestroyOnLoad。我们需要更改 Canvas 上的元素。DDOL 保存了场景中的最后一个位置,但我们根本无法更改对象。

你能得到一些建议吗?

谢谢。

c# unity-game-engine

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

下载大文件

通过下载文件UnityEngine.WWW,我收到错误

OverflowException:Number溢出.

我发现错误是由结构本身引起的,因为字节数组比int.MaxValue可以分配更多的字节(~2GB).

通过返回数组来触发错误www.bytes,这意味着框架可能以其他方式存储数组.

如何以其他方式访问下载的数据,还是有更大的文件替代?

public IEnumerator downloadFile()
{
    WWW www = new WWW(filesource);

    while(!www.isDone)
    {
        progress = www.progress;
        yield return null;
    }

    if(string.IsNullOrEmpty(www.error))
    {
        data = www.bytes; // <- Errormessage fired here
    }
}
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

5
推荐指数
1
解决办法
3107
查看次数

Unity VR 中的滑块

我们正在开发适用于 Go 的视频播放器应用程序。我们构建了一个简单的 raycaster 脚本来在用户指向 UI 按钮元素并拉动触发器时触发 onClick 事件:

bool triggerPulled = OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger);
    if (Physics.Raycast(transform.position, transform.forward, out hit, 1000))
    {

        if ( triggerPulled )
        {

            // if we hit a button
            Button button = hit.transform.gameObject.GetComponent<Button>();

            if (button != null)
            {
                button.onClick.Invoke();
            }

        }
....
    }
Run Code Online (Sandbox Code Playgroud)

我们真的希望能够使用激光笔和按钮来操作 UI 滑块,但不清楚是否存在我们可以为适当行为触发的类似事件。我们可以调用onValueChanged来改变值,但这并不能真正为我们提供我们想要的滑动行为,只有在我们知道最终位置后才让我们设置新值。

有没有人对如何解决这个问题有好主意?

c# unity-game-engine virtual-reality

5
推荐指数
1
解决办法
3667
查看次数