如何从资源文件夹加载图像并将该图像设置为UI Image的源图像?
我在Unity中创建的游戏存在问题.玩家控制一个被一大群僵尸攻击的角色.我已经为所有的僵尸创造了一个产卵器并且效果很好,唯一的问题是,一旦玩家杀死一个僵尸,所有的僵尸都会从游戏世界中消失.我发布了附在下面每个僵尸上的敌人脚本.我无法弄清楚为什么每个僵尸都被摧毁而不仅仅是被攻击的僵尸.任何帮助都会很棒!
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public static float Damage = 10.0f;
public static float Health = 10.0f;
public Transform target;
public float Speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Destroy the enemy if it's health reaches 0
if(Health <= 0){
Destroy(this.gameObject);
Debug.Log ("Enemy Destroyed!");
}
//Constantly move the enemy towards the centre of the gamespace (where the base is) …Run Code Online (Sandbox Code Playgroud) 所以我创建了这个对象,它应该在我触摸游戏对象时在屏幕上加载游戏。但是,当我运行它时,出现此运行时错误
场景 'A' (-1) 无法加载,因为它尚未添加到构建设置或资产包尚未加载。要将场景添加到构建设置,请使用菜单 File->Build Settings... UnityEngine.Application:LoadLevel(String) NewBehaviourScript:OnCollisionEnter(Collision)(在 Assets/NewBehaviourScript.cs:10)
这是我的代码:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name =="Hurt")
{
Application.LoadLevel("A");
}
}
}
Run Code Online (Sandbox Code Playgroud) scripting runtime-error unity-game-engine assetbundle unity5
我原本以为必须知道使用Unity开发的C#和UnityScript.但现在看来我只需要知道一个.所以,
我正在使用该WebClient.DownloadFileAsync()方法,并想知道如何将参数传递给WebClient.DownloadFileCompleted事件(或任何其他事件),并在调用的方法中使用它.
我的代码:
public class MyClass
{
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += DoSomethingOnFinish;
Uri uri = new Uri(downloadPath + "\" + fileNameID );
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
}
}
Run Code Online (Sandbox Code Playgroud)
如何将参数传递给DoSomethingOnFinish()?
该模型
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GetPeopleResult
{
public List<Person> people { get; set; }
public GetPeopleResult()
{
this.people = new List<People>();
}
public static GetPeopleResult CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<GetPeopleResult>(jsonString);
}
}
[System.Serializable]
public class Person
{
public long id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string displayImageUrl { get; set; }
public Person()
{
}
public static Person CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Person>(jsonString); …Run Code Online (Sandbox Code Playgroud) 我偶然发现了一些我从未想过的问题,但是...确实如此。我正在使用unity 5.5编写visualiazion / simulation工具箱。
使用的模型已经导入,并且通过编辑器设置了动画剪辑。这是我的动画状态控制器的外观:
我想在DropdownMenu中显示AnimationStates。每当用户选择一项时,当前动画状态都应设置为所选状态。
eG用户选择了自动播放:将状态设置为剪辑自动播放,然后设置为SimpleRotate,然后设置为ComplexRotate,然后再次设置为自动播放(只是一个简单的循环)。
但是,如果用户选择“空闲”,“ ComplexRotate 0”或“ SimpleRotate 0”,则动画应播放一次,然后停留在剪辑的结尾。(就像我在动画控制器中所做的一样。
这是我想要的一个相当伪的版本:
//Retrieve all states to populate the dropdown:
List<AnimationState> states = myAnimationController.GetAllStates();
foreach(AnimationState state in states)
Dropdown.add(state.name)
//callback
OnDropdownIndexChanged(int item)
{
string stateName = ..... //removed for simplicity
myAnimationController.setState(stateName)
}
Run Code Online (Sandbox Code Playgroud)
最重要的是,如果我可以检查FROM和TO状态之间的转换,那就太好了。(自动播放是应该在循环的下拉菜单中显示的唯一状态)
可以通过自定义动画控制器实现此功能吗?还是我错过了什么?
我正在使用Unity 5.4.1f1个人版并在Windows独立版本上工作.我想在我的游戏中使用多个显示器.我有2个摄像头,并设置一个摄像头的目标显示器显示1和第二个摄像头的目标显示器显示2.
此外,我已激活显示,但Display.displays.Length始终在编辑器和构建上.
因此它不会在第二个屏幕上显示任何内容.我该怎么办才能在第二个屏幕上显示?
当我从编辑器运行游戏并保存加载数据时,我发现数据在:C:\Users\User\AppData\LocalLow\DefaultCompany\projectname\data.
当我构建它并获得可执行文件时,该数据仍可正常加载,但如果我保存并重新启动,则不会保存.但是当我从编辑器中启动它时它会这样做.
这是我的代码中的错误,还是我不从Unity Editor运行它的其他地方的文件?
稍后当我导出游戏以启动游戏时,我会拥有持久数据I Json文件,这些文件必须与游戏一起使用才能运行.
为清楚起见,这里是处理Json的保存/加载的类:
public class DataHandler
{
//Save Data
public static void saveData<T>(T dataToSave, string dataFileName)
{
string tempPath = Path.Combine(Application.persistentDataPath, "data");
tempPath = Path.Combine(tempPath, dataFileName + ".txt");
//Convert To Json then to bytes
string jsonData = JsonUtility.ToJson(dataToSave, true);
byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
}
//Debug.Log(path);
try
{
File.WriteAllBytes(tempPath, jsonByte);
Debug.Log("Saved Data to: " + tempPath.Replace("/", "\\"));
}
catch (Exception e)
{ …Run Code Online (Sandbox Code Playgroud) unity5 ×10
c# ×6
json ×2
scripting ×2
assetbundle ×1
asynchronous ×1
unity3d-gui ×1
unityscript ×1