我想在 AR 相机中播放视频。我总共有 10 个视频和一个视频播放器。我正在从服务器下载视频播放器作为资产包,名称为 videoplayer.unit3d 并存储到 SD 卡中。当我扫描 imageTarget 时,我正在使用AssetBundle.LoadFromFile()函数读取视频资产包文件,并且第一次它工作正常。
如果我扫描第二个 imageTarget 它会显示以下错误
“无法加载,因为已经加载了另一个具有相同文件的 AssetBundle”
我试过 a bundle.Unload(true);andCaching.cleanchache()但它不工作抛出同样的错误。也试过bundle.Unload(false);
private void loadObject(string resourcePath, string objectName, TrackableBehaviour trackableBehaviuor, string videoUrl)
{
Debug.Log("Resource path " + resourcePath + " objectName " + objectName);
Debug.Log("Video Url from sd card " + videoUrl);
FileInfo fileInfo = new FileInfo(resourcePath);
if (!fileInfo.Exists)
return;
Debug.Log("File is present");
AssetBundle bundle = AssetBundle.LoadFromFile(resourcePath, 0, 0);//www.assetBundle;
Debug.Log("Bundle data is " + bundle);
if (bundle …Run Code Online (Sandbox Code Playgroud)我如何根据统一检查器中的其他变量值隐藏变量。基本上想象一下:如果我有一个名为“CanSprint”的布尔值和一个名为“SprintSpeed”的浮点数,那么我想这样做,以便当布尔值为真时,浮点数显示,但当布尔值为假时,浮点数隐藏。这只是为了更整洁一点。谢谢!
我正在学习C#,Unity我创建了一个简单的游戏,您可以通过单击"C"键(将变为绿色)更改播放器的颜色(默认情况下为红色).代码可以工作,但问题是,我不知道如何创建一个代码,通过使用相同的键("C")再次将绿色更改为红色.我知道的唯一选择是创建另一个if但使用不同的按钮.这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubecolor : MonoBehaviour {
// Use this for initialization
void Start () {
gameObject.GetComponent<Renderer> ().material.color = Color.red;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.C))
gameObject.GetComponent<Renderer> ().material.color = Color.green;
}
}
Run Code Online (Sandbox Code Playgroud) 我有4种颜色。我想让玩家不能连续 2 次使用相同的颜色。当玩家与物体碰撞时,RandomColor()调用 。所以这个函数在游戏过程中被多次调用,有时玩家不会改变他的颜色。
using UnityEngine;
public class ColorManager : MonoBehaviour {
public SpriteRenderer player;
public string playerColor;
public Color[] colors = new Color[4];
private void Awake()
{
RandomColor();
}
public void RandomColor()
{
int index = Random.Range(0, 4);
switch (index)
{
case 0:
player.color = colors[0]; //colors[0] is orange
playerColor = "orange";
break;
case 1:
player.color = colors[1]; //colors[1] is pink
playerColor = "pink";
break;
case 2:
player.color = colors[2]; //colors[2] is blue
playerColor = "blue";
break; …Run Code Online (Sandbox Code Playgroud) 我正在 Unity 中编写一个简单的代码,以检查我是否能够通过我的应用程序访问网站。这是我写的代码:
IEnumerator CheckInternetPing()
{
WWW wwwInternet = new WWW("http://google.com");
yield return wwwInternet;
if (wwwInternet.bytesDownloaded == 0)
{
//yield return new WaitForSeconds(1f);
Debug.Log("Not Connected to Internet");
}
else
{
Debug.Log("Connected to Internet");
internetMenu.SetActive(false);
}
}
Run Code Online (Sandbox Code Playgroud)
我发现了一个错误,如果我在互联网上运行它,它会显示“已连接”,但是当我关闭互联网并立即运行该应用程序时,它不会记录任何内容。仅当我再次重新启动应用程序时,它才会显示“未连接”。有谁知道为什么它在第一次没有记录任何内容?谢谢
我正在尝试将浮点数减少一个时间值,我正在使用Unity并停止时间Time.timeScale = 0f;因此无法Time.deltaTime在while循环中使用'Time.realtimeSinceStartup',我从全局脚本中读取主卷变量玩家可以在游戏中设置0 - 1,所以说我读入0.6并且我想在2秒内将音量降低到0我如何获得百分比以保持减少音量?
这是我的代码..
private IEnumerator VolumeDown ()
{
float volumeIncrease = globalVarsScript.musicVolume;
float volumePercentage = ??;
float newLerpEndTime = Time.realtimeSinceStartup + 2f;
while (Time.realtimeSinceStartup < newLerpEndTime)
{
audio.volume = volumeIncrease;
volumeIncrease -= volumePercentage;
yield return null;
}
}
Run Code Online (Sandbox Code Playgroud)
对不起,我只是无法获得'volumePercentage'
谢谢.
我对内存和优化比较陌生。我的任务是优化一个现有项目,在查看 Unity 中的分析器时,我发现有一个函数会生成一堆东西供 GC 清理。
该函数是这样的:
public bool AnyCanvasOpen()
{
for (int i = 0; i < canvasList.Count; i++)
if (canvasList[i].activeSelf && canvasList[i].name != "MainUICanvas")
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
其中 canvasList 只是游戏对象的列表。我的 282B 分配可能是什么原因?
我有一个函数,检查游戏对象是否可以看到另一个游戏对象,而不会阻止其视图:
public bool CheckVision(GameObject target)
{
RaycastHit2D ray = (Physics2D.Raycast(transform.position, target.transform.position - transform.position, m_VisionRange));
if(ray.collider.name == target.name)
{
Debug.DrawRay(transform.position, target.transform.position - transform.position);
return true;
}
else
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是我不希望它与设置了"isTrigger"标志的碰撞器碰撞; 我希望raycast能够忽略它们.有任何想法吗?
在尝试将数组从C++传递给C#时,我看到了一个非常奇怪的问题.我正在使用Marshal.Copy(具体来说:https://msdn.microsoft.com/en-us/library/a53bd6cz(v = vs1010 ).aspx ).
问题:从C++到C#的float数组NaN在结果数组中产生了一些.
(注意:我在Unity游戏引擎的上下文中工作)
码
示例C++代码:
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API getSomeFloats(float** points, int* count) {
std::vector<float> results;
std::vector<SOME_TYPE> key_points = <SOME_POINTS>
for (auto iter = key_points.begin(); iter < key_points.end(); iter++) {
results.push_back(static_cast<float>(iter->pt.x));
results.push_back(static_cast<float>(iter->pt.y));
}
*points = results.data();
*count = results.size();
//<Print results to csv here>
return true;
}
Run Code Online (Sandbox Code Playgroud)
示例C#代码:
[DllImport("NativePlugin")]
private static extern bool getSomeFloats (ref IntPtr ptrResultItems, ref int resultItemsLength);
private static float[] getFloatArrayFromNative() {
IntPtr ptrResultItems = IntPtr.Zero;
int …Run Code Online (Sandbox Code Playgroud) 我添加了基于 的前向力GetAxis ("Vertical")和高达 的速度调节器normal max speed。
我还允许“速度提升”,在正常附加力的基础上增加更多的力,最高可达boosted max speed。
我遇到的困难是一旦升压期结束就减慢物体的速度
因此,基本上,如果您将速度提升到比正常最大速度更快的速度,并且您无法再进行速度提升,请将物体减慢回正常最大速度。
这是我到目前为止得到的:
float velocity;
float magnitude = myRigidBody.velocity.magnitude;
float vertical = Input.GetAxis ("Vertical");
if (Input.GetKeyUp(KeyCode.LeftShift)) {
boost = false;
}
if (Input.GetKeyDown (KeyCode.LeftShift)) {
boost = true;
}
if (!boost) {
velocity = vertical * speed;
Vector3 force = myRigidBody.transform.forward * velocity;
if (magnitude + force.sqrMagnitude < normalMaxSpeed) {
myRigidBody.drag = 0f;
myRigidBody.AddForce (force, ForceMode.Impulse);
} else if (magnitude + force.sqrMagnitude >= maxTrust + …Run Code Online (Sandbox Code Playgroud) c# ×10
arrays ×1
assetbundle ×1
c++ ×1
colors ×1
httprequest ×1
inspector ×1
interop ×1
memory ×1
random ×1
raycasting ×1
repeat ×1
string ×1
unity2d ×1
while-loop ×1