更改粒子系统 material Unity 3D 脚本
Change particle system material Unity 3D script
我正在尝试为我的对象创建粒子发射,这些对象实际上是国际象棋游戏中的棋子。我使用 Inspector 为粒子系统更改了粒子 material,它工作正常,但是,当我尝试使用脚本更改它时,什么也没有发生(黑色 pawn 的粒子保持粉红色)。
我不确定脚本有什么问题,是 material 问题的变化还是我需要为 material 设置路径。如果有人提出解决方案,我会很高兴!
这是脚本:
public class BlackPawnParticleSystem : MonoBehaviour
{
private Material m_Material;
private ParticleSystemRenderer psr;
private void Start()
{
var ps = GetComponent<ParticleSystem>();
m_Material = GetComponent<Renderer>).material;
psr = GetComponent<ParticleSystemRenderer>);
psr.material = Resources.GetBuiltinResource<Material>("BlackBishop.mat");
}
这是它的样子:
编辑:
代码更改:
public class BlackPawnParticleSystem : MonoBehaviour
{
private Material m_Material;
private ParticleSystemRenderer psr;
private void Start()
{
var ps = GetComponent<ParticleSystem>();
m_Material = GetComponent<Renderer>().material;
psr = GetComponent<ParticleSystemRenderer>();
psr.material = Resources.Load<Material>("BlackBishop");
}
我的 material 的位置:
material 的检查员:
附加到对象的脚本:
问题出在这一行:Resources.GetBuiltinResource<Material>("BlackBishop.mat")
返回空值。 Resources.GetBuiltinResource
函数用于加载 Unity 内置的资源,而不是您创建的资源。例如,"Sprites-Default.mat"。没有名为 "BlackBishop.mat" 的内置 material 所以它 returns null.
要加载您创建的 material,请使用 Resources.Load
函数。
首先,创建一个名为 "Resources" 的文件夹,然后将您的 "BlackBishop.mat" material 放在那里。您现在可以按如下方式加载它:
Resources.Load<Material>("BlackBishop");
请注意我没有在名称中包含“.mat”。使用 Resources.Load
函数时不包含扩展名。如果这样做,它将找不到它。
编辑:
But still nothing happens on my game scene
请慎重:
1。正如我在原来的回答中所说,您 必须 创建一个名为 [=98= 的文件夹] 并且必须将 "Black Bishop" material 放入该文件夹中。你没有这样做,它不应该工作。这个文件夹的拼写必须正确,所以直接从这个答案中复制它。同样,它应该命名为 "Resources".
2。你的 material 被命名为 "Black Bishop" 而不是 "BlackBishop"。参见space。请在代码中修复此问题。那应该是Resources.Load<Material>("Black Bishop");
而不是Resources.Load<Material>("BlackBishop");
。
3。您的 material 当前正在使用 "Standard" 着色器。将其更改为粒子着色器。您可以通过选择 Material 并将着色器从 "Standard" 更改为 "Particles/Alpha Blended Premultiply" 来实现。而已。您的问题现在应该已经解决了。
额外:
我注意到您正在使用文件夹来组织您的资源。你的 "Black Bishop.mat" 在 Arcane Cyber --> Chess --> Materials --> Pieces --> Plastic 里面。我建议您将 "Materials" 文件夹移动到上面 #1 中创建的 "Resources" 文件夹。
现在,你的 "Black Bishop.mat" 应该在 Assets --> Resources --> Materials --> Pieces --> Plastic.
您现在可以将其加载为:
Resources.Load<Material>("Materials/Pieces/Plastic/Black Bishop");
如果您仍然遇到问题,请再次更新您的代码和新的 Inspector 图像。请记住,拼写很重要。
为什么不直接用您需要的材料制作一个 SerializedField?如果您不希望材料和轨迹是随机的,请将它们设置到阵列中您需要的情况下的确切位置。
我是这样做的:
using UnityEngine;
public class CorrectParticles : MonoBehaviour
{
private static ParticleSystem correctPart;
private static ParticleSystemRenderer correctRend;
public static CorrectParticles Instace;
[SerializeField] Material[] mainMaterials;
[SerializeField] Material[] trailMaterials;
private void Awake()
{
Instace = this;
}
void Start()
{
correctPart = GetComponent<ParticleSystem>();
correctRend = GetComponent<ParticleSystemRenderer>();
correctPart.Stop();
}
public void StartParticles()
{
int mainMaterial = Random.Range(0, mainMaterials.Length);
correctRend.material = mainMaterials[mainMaterial];
int trailMaterial = Random.Range(0, trailMaterials.Length);
correctRend.trailMaterial = trailMaterials[trailMaterial];
correctPart.Play();
}
public void StopParticles()
{
correctPart.Stop(withChildren: true, stopBehavior: ParticleSystemStopBehavior.StopEmittingAndClear);
}
}
然后我可以根据需要在其他脚本上启动和停止粒子系统。
CorrectParticles.Instace.StartParticles();
或
CorrectParticles.Instace.StopParticles();
我正在尝试为我的对象创建粒子发射,这些对象实际上是国际象棋游戏中的棋子。我使用 Inspector 为粒子系统更改了粒子 material,它工作正常,但是,当我尝试使用脚本更改它时,什么也没有发生(黑色 pawn 的粒子保持粉红色)。
我不确定脚本有什么问题,是 material 问题的变化还是我需要为 material 设置路径。如果有人提出解决方案,我会很高兴!
这是脚本:
public class BlackPawnParticleSystem : MonoBehaviour
{
private Material m_Material;
private ParticleSystemRenderer psr;
private void Start()
{
var ps = GetComponent<ParticleSystem>();
m_Material = GetComponent<Renderer>).material;
psr = GetComponent<ParticleSystemRenderer>);
psr.material = Resources.GetBuiltinResource<Material>("BlackBishop.mat");
}
这是它的样子:
编辑:
代码更改:
public class BlackPawnParticleSystem : MonoBehaviour
{
private Material m_Material;
private ParticleSystemRenderer psr;
private void Start()
{
var ps = GetComponent<ParticleSystem>();
m_Material = GetComponent<Renderer>().material;
psr = GetComponent<ParticleSystemRenderer>();
psr.material = Resources.Load<Material>("BlackBishop");
}
我的 material 的位置:
material 的检查员:
附加到对象的脚本:
问题出在这一行:Resources.GetBuiltinResource<Material>("BlackBishop.mat")
返回空值。 Resources.GetBuiltinResource
函数用于加载 Unity 内置的资源,而不是您创建的资源。例如,"Sprites-Default.mat"。没有名为 "BlackBishop.mat" 的内置 material 所以它 returns null.
要加载您创建的 material,请使用 Resources.Load
函数。
首先,创建一个名为 "Resources" 的文件夹,然后将您的 "BlackBishop.mat" material 放在那里。您现在可以按如下方式加载它:
Resources.Load<Material>("BlackBishop");
请注意我没有在名称中包含“.mat”。使用 Resources.Load
函数时不包含扩展名。如果这样做,它将找不到它。
编辑:
But still nothing happens on my game scene
请慎重:
1。正如我在原来的回答中所说,您 必须 创建一个名为 [=98= 的文件夹] 并且必须将 "Black Bishop" material 放入该文件夹中。你没有这样做,它不应该工作。这个文件夹的拼写必须正确,所以直接从这个答案中复制它。同样,它应该命名为 "Resources".
2。你的 material 被命名为 "Black Bishop" 而不是 "BlackBishop"。参见space。请在代码中修复此问题。那应该是Resources.Load<Material>("Black Bishop");
而不是Resources.Load<Material>("BlackBishop");
。
3。您的 material 当前正在使用 "Standard" 着色器。将其更改为粒子着色器。您可以通过选择 Material 并将着色器从 "Standard" 更改为 "Particles/Alpha Blended Premultiply" 来实现。而已。您的问题现在应该已经解决了。
额外:
我注意到您正在使用文件夹来组织您的资源。你的 "Black Bishop.mat" 在 Arcane Cyber --> Chess --> Materials --> Pieces --> Plastic 里面。我建议您将 "Materials" 文件夹移动到上面 #1 中创建的 "Resources" 文件夹。
现在,你的 "Black Bishop.mat" 应该在 Assets --> Resources --> Materials --> Pieces --> Plastic.
您现在可以将其加载为:
Resources.Load<Material>("Materials/Pieces/Plastic/Black Bishop");
如果您仍然遇到问题,请再次更新您的代码和新的 Inspector 图像。请记住,拼写很重要。
为什么不直接用您需要的材料制作一个 SerializedField?如果您不希望材料和轨迹是随机的,请将它们设置到阵列中您需要的情况下的确切位置。
我是这样做的:
using UnityEngine;
public class CorrectParticles : MonoBehaviour
{
private static ParticleSystem correctPart;
private static ParticleSystemRenderer correctRend;
public static CorrectParticles Instace;
[SerializeField] Material[] mainMaterials;
[SerializeField] Material[] trailMaterials;
private void Awake()
{
Instace = this;
}
void Start()
{
correctPart = GetComponent<ParticleSystem>();
correctRend = GetComponent<ParticleSystemRenderer>();
correctPart.Stop();
}
public void StartParticles()
{
int mainMaterial = Random.Range(0, mainMaterials.Length);
correctRend.material = mainMaterials[mainMaterial];
int trailMaterial = Random.Range(0, trailMaterials.Length);
correctRend.trailMaterial = trailMaterials[trailMaterial];
correctPart.Play();
}
public void StopParticles()
{
correctPart.Stop(withChildren: true, stopBehavior: ParticleSystemStopBehavior.StopEmittingAndClear);
}
}
然后我可以根据需要在其他脚本上启动和停止粒子系统。
CorrectParticles.Instace.StartParticles();
或
CorrectParticles.Instace.StopParticles();