当您调用函数LoadScene()时,它会立即切换场景,还是只是发出需要更改场景的信号?LoadScene()的文档没有说明.
我正在使用的具体示例如下所示:
LoadScene(levelName);
ItemBox newBox = (ItemBox)Instantiate(...);
Run Code Online (Sandbox Code Playgroud)
因此,使用上面的代码,newBox是否存在于我们刚加载的场景中,或者是否会在旧场景中创建,然后在加载新级别时销毁.
我想在Unity项目中使用C#插件.该插件应该充当服务器,它将从客户端获取值,以便我能够使用这些值进行进一步处理.问题是服务器有无限循环.无限循环导致Unity挂起.怎么办呢?
编辑:我附加了服务器程序的代码片段.在我看来,有两点可能导致问题.无限循环和程序暂停的点,如代码所示:
void networkCode()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1755);
// Create a TCP/IP socket.
listener = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// …Run Code Online (Sandbox Code Playgroud) 对于部署到Android设备的应用,如何在Unity中以编程方式将显示设置为立体?
我想要一个UI菜单,用户可以在"VR模式"和普通模式之间切换.我默认不想要VR模式,因为它应该是运行时的选项.我知道在构建设置中有"虚拟现实支持"的设置,但同样,我不希望默认启用此功能.
我试图仅使用6个音频剪辑重新创建全系列的吉他.
我想有一种设置音频剪辑频率的方法,但audio.frequency只返回基于压缩格式而不是实际音调的音频频率.
我知道我可以阅读GetSpectrumData,但是这个解决方案相当复杂,需要进行一些傅里叶变换分析或类似的东西.
影响音高,很容易改变音调,所以我可以上下,但是有办法弄清楚使用的步骤是什么.
void Update ()
{
CheckAudio(KeyCode.Q, 1.0f);
CheckAudio(KeyCode.W, 1.1f);
CheckAudio(KeyCode.E, 1.2f);
CheckAudio(KeyCode.R, 1.3f);
CheckAudio(KeyCode.T, 1.4f);
}
void CheckAudio(KeyCode key, float pitch)
{
if (Input.GetKeyDown (key))
{
audio.pitch = pitch;
audio.Play ();
}
}
Run Code Online (Sandbox Code Playgroud)
我听说听起来不对劲.
知道初始音调E4 329.63Hz,音高为1是否有任何影响音高的方程式,我会得到下一个键F4 349.23Hz(或足够接近)?
还必须考虑Unity AudioSource将音调限制在-3/3范围内(我认为这超出了需要).
编辑:添加一些个人研究.似乎音高1是初始音符,设置为2给出相同的音高一个八度.
由于半音阶(钢琴上的所有黑白音符)都是12个键,我假设每个步骤使用1/12应该这样做.
听起来很接近,但我觉得它不太对劲.这是新代码:
[SerializeField] private AudioSource audio;
float step = 1f/12f;
KeyCode[]keys = new KeyCode[]{
KeyCode.Q, KeyCode.W,KeyCode.E,KeyCode.R,KeyCode.T,
KeyCode.Y, KeyCode.U, KeyCode.I, KeyCode.O, KeyCode.P,
KeyCode.A, KeyCode.S, KeyCode.D
};
void Update ()
{
float f = 0.0f;
foreach (KeyCode key in …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".无论如何有任何想法为什么会发生这种情况?
我一直在使用Unity中的保存和加载,我将序列化的类保存到文件中.我有一个Serializable类:
[Serializable]
class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
}
Run Code Online (Sandbox Code Playgroud)
并将其保存到文件A-OK.我可以加载它没有错误,但如果我想稍后添加:
[Serializable]
class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
public int extra = 0;
}
Run Code Online (Sandbox Code Playgroud)
我运行我的脚本它给了我一个反序列化错误,我完全理解,当我将反序列化文件转换为我的新类'保存'时,我添加的新变量不存在,它给了我错误.
我在商店中修复资产时发现了这个错误,我知道一个修复可以只是更改文件名,因此创建了一个新文件,但我不想只删除之前保存的内容.
所以我的问题是,如果我想在我的序列化类中添加更多变量,如果人们要更新资产,我将如何捕获并调整旧版本的变量?
谢谢!
我需要将一个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) 我注意到在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) 我目前遇到了C#中更新编码的问题.我正在制作一本教科书"3.x Game Development Essentials",目前正在尝试制作一个阵列,该阵列将分配纹理,这将显示电池充电的进度.教科书要我创建一个GUITexture,但是这在之前的Unity更新中已经过时了,所以我创建了一个创建Canvas和子游戏对象的UI Image.虽然这解决了屏幕上的问题,但编码时情况更糟.这本书要我创建一个由五个纹理组成的数组(四种状态,加上原始的空纹理).目标是每当角色拿起一个电池时,它会在UI Canvas中反映出来.现在,这是我得到goobery的地方:
这是本书指定在我的Inventory脚本上实现的原始编码:
public Texture2D [] hudCharge;
public GUITexture charge HudGUI;
Run Code Online (Sandbox Code Playgroud)
我最终不得不松鼠去处理旧代码,并尝试了这个:
using UnityEngine.UI;
public class Inventory : MonoBehaviour{
public static int charge = 0
public UnityEngine.AudioClip collectSound;
public UnityEngine.Texture2D[] hudCharge;
public Image chargeHudGUI;
Run Code Online (Sandbox Code Playgroud)
现在我们变得更奇怪了.因为本书使用的是过时的代码,所以它要我使用电荷值从数组中选择一个纹理.因此,它要我输入:
chargeHudGUI.texture = hudCharge[charge];
Run Code Online (Sandbox Code Playgroud)
目标是我们正在解决分配给chargeHudGUI变量的GUITexture对象,特别是它的纹理属性.嗯,这很时髦但是再也没有GUITexture了,那么它离我而去的地方呢?它让我这样:
chargeHudGUI.Image = hudCharge[charge].
Run Code Online (Sandbox Code Playgroud)
我一直在浏览每个线程,我可以尝试找出该做什么,但似乎没有解决这个特殊问题.有没有人有任何指导导航这个废话?我已经在这个游戏上工作了一段时间,我决心完成它.我想学习如何使用Visual Studio和Unity3D.我感谢任何帮助!
c# user-interface unity-game-engine visual-studio guitexture
c# ×9
unity5 ×2
audio ×1
audioclip ×1
export ×1
frequency ×1
guitexture ×1
io ×1
javascript ×1
sockets ×1
unityscript ×1