Tag: unity3d

Unity 5.5过时的粒子系统代码

之前5.5粒子系统变量可以通过ParticleSystem访问并进行读/写。 现在它们是通过ParticleSystem.MainModule访问的,因此很多代码已经过时了。 API更新程序无法解决大多数问题。 我已经阅读了新文档,但我无法弄清楚应该如何使用新的变量类型。 例如,在JetParticleEffect.cs中,此行会引发警告: // set the original properties from the particle system m_OriginalLifetime = m_System.startLifetime; 警告声明:’ParticleSystem.startLifetime’已过时:’不推荐使用startLifetime属性。 请改用main.startLifetime或main.startLifetimeMultiplier。 我尝试过以下方法: m_OriginalLifetime = m_System.main.startLifetime; // error: Cannot implicitly convert type ‘UnityEngine.ParticleSystem.MinMaxCurve’ to ‘float’ 我相信答案与minMaxCurve 常量变量有关,因为这会编译: m_OriginalLifetime = m_System.main.startLifetime.constant; 但是文档中几乎没有解释。 任何人都可以对此有所了解吗? 此外,新的乘数适合哪里? 我假设你以前可以做到这一点: particle.startSize *= myMultiplier ……你现在应该这样做吗? particle.main.startSizeMultiplier = myMultiplier

Unity从文件夹加载媒体并在RawImage上显示

我正在尝试在Unity中创建一个媒体播放器,从静态文件夹中读取所有媒体文件并播放所有媒体(图像静态持续时间,video长度为video)。 首先,我试图让它与图像一起工作。 我是Unity的新手,不熟悉C#。 我能够将所有媒体文件源(图像)都放到一个数组中,但接下来我需要将它们转换为纹理并放在RawImage组件上。 我坚持这部分。 如果我有src(例如C:\ medias \ img1.jpg)那么我如何将它作为图像放在RawImage组件上? 我的代码 – > using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEditor; using System; using System.IO; using System.Linq; public class Player : MonoBehaviour { // Use this for initialization void Start () { DirectoryInfo dir = new DirectoryInfo(@”C:\medias”); string[] extensions = new[] { “.jpg”, “.JPG”, […]

碰撞后立即停止刚体运动/旋转

我希望我的球体从一个位置跳到另一个位置但不希望它随后翻译。 我无法弄清楚如何做到这一点。 这是我的代码: void Update() { if (!thrown && ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) || Input.GetMouseButtonDown(0))) { rb.isKinematic = false; rb.AddForce(new Vector3(0.0f, 15.0f, 5.0f) ); thrown = true; } }

比较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. […]

按名称,标签或图层查找非活动的GameObject

首先,我需要停用游戏对象,然后在10秒后激活它,所以我认为协同程序是合适的: IEnumerator BarDeactivate(float sec) { yield return new WaitForSeconds(sec); var obj = GameObject.Find(“OBJ”); obj.SetActive(false); } IEnumerator BarReactivate(float sec) { yield return new WaitForSeconds(sec); var obj = transform.Find(“OBJ”); obj.SetActive(true); } 显然,我不能再使用GameObject.Find所以我使用transform.Find来查找不活动的游戏对象,但当然.SetActive现在不起作用,因为obj实际上不是游戏对象…… 如何转换找到的变换,以便可以再次设置它? 我试过obj.gameObject.SetActive(true)但它一定是错的,因为对象不会obj.gameObject.SetActive(true) ……

Unity Physics2D.Raycast击中了自己

我正在尝试使用Physics2D.Raycast来检查玩家是否在地面上(我知道还有其他方法来检查玩家是否在地面但我认为光线投射是最可靠的)。 问题是在我的场景中它将玩家本身作为命中返回,我真的不明白为什么以及我该怎么做。 我的代码(在PlayerController )如下: public bool IsGrounded () { Bounds bounds = this.playerCollider.bounds; Vector3 rayOrigin = bounds.center; rayOrigin.y -= bounds.extents.y; RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.down, 0.1f); if (hit.collider != null) { Debug.Log (“Collider is: ” + hit.collider.name); } return hit.collider != null; } 我可以使用以下方法调试铸造射线: Debug.DrawLine (rayOrigin, new Vector3 (rayOrigin.x, rayOrigin.y – 0.1f, rayOrigin.z), Color.magenta); […]

如何通过脚本使Texture2D可读

我想让用户能够解码从图库加载的QR图像,我找到了一个插件来探索并加载图像作为texture2D,但要解码那个QR码,Texture2D必须是可读/可写的,我检查插件,对于Android,它正在使用jar进行探索和加载,在IOS平台中,它使用的是打包的库,因此我无法访问lib的代码, 我已经搜索了答案,最大的解决方案是在Unity检查器中更改纹理的导入设置,但由于这是由代码加载的纹理,因此没有可用的检查器设置,所以我的问题是: 有没有办法让这个加载的纹理可以通过代码读/写? 无需访问lib代码? 谢谢 这是可以通过此插件获取纹理的代码 void OnImageLoad(string imgPath, Texture2D tex, ImageAndVideoPicker.ImageOrientation imgOrientation) { Debug.Log(“Image Location : ” + imgPath); Debug.Log(“Image Loaded : ” + imgPath); texture = tex; Texture2D readableText = new Texture2D(tex.width, tex.height); readableText.LoadImage(tex.GetRawTextureData()); string url = QRCodeDecodeController.DecodeByStaticPic(readableText); StartCoroutine(GetSceneAndLoadLevel(url)); } 正如你所看到的,我试过这个答案,但没有运气。 以下是Android显示的错误: 06-23 21:47:32.853: I/Unity(10557): (Filename: D Line: 0) 06-23 21:47:33.784: E/Unity(10557): Texture […]

使用Unity在C#中发送http请求

如何使用Unity在C#中发送HTTP GET和POST请求? 我想要的是: 在post请求中发送json数据(我使用Unity序列化器,因此不需要新的,我只想在post数据中传递字符串并且能够将ContentType设置为application / json); 获得响应代码和正文没有任何问题; 在不阻止ui渲染的情况下完成异步操作。 我尝试过的: 使用HttpWebRequest / HttpWebResponse实现,但它太难和低级别(如果我找不到更好的东西,我将不得不使用它); 使用统一WWW,但它不符合我的要求; 使用NuGet的一些外部包 – Unity不接受它们:( 大多数问题都是使用线程,我在C#中没有足够的经验。 我使用的文本编辑器是Intellij Rider。

Unity EventManager与委托而不是UnityEvent

我正在使用UnityEvent查找此Manager的 c#委托版本。 我不想使用它,因为UnityEvent在大多数时候比C#事件慢。 有关如何实现这一点的任何线索?

如何在Unity中序列化和保存GameObject

我有一个游戏,玩家拿起一个武器,然后将其作为GameObject变量放置到我的玩家名为“MainHandWeapon”,我试图通过场景更改保存该武器,所以我试图保存它。 我如何处理这个如下: public class Player_Manager : Character, Can_Take_Damage { // The weapon the player has. public GameObject MainHandWeapon; public void Save() { // Create the Binary Formatter. BinaryFormatter bf = new BinaryFormatter(); // Stream the file with a File Stream. (Note that File.Create() ‘Creates’ or ‘Overwrites’ a file.) FileStream file = File.Create(Application.persistentDataPath + “/PlayerData.dat”); // […]