新版播放粒子

添加粒子:

void PopStarManager::AddParticlePool()
{
	CCLOG("popstar---AddParticlePool----1");	
	m_ParticleBatchNodeStar =CCParticleBatchNode::create("pop_stars.png");
	m_ParticleBatchNodeStar->retain();

	if(m_dataGameParticle==NULL)
	{
		m_dataGameParticle = CCDictionary::create();
		m_dataGameParticle->retain();
		vector<string> pArray;
		pArray.push_back("pop_star1000.plist");
		pArray.push_back("pop_star1001.plist");
		pArray.push_back("pop_star1002.plist");
		pArray.push_back("pop_star1003.plist");
		pArray.push_back("pop_star1004.plist");
		
		//CCObject* obj=NULL;
		for(int i=0;i<(int)pArray.size();i++)
		{		
			CCParticleSystemQuad* particleSystem = CCParticleSystemQuad::create(pArray[i].c_str());
			m_dataGameParticle->setObject(particleSystem,pArray[i]);
		}
	}
}

播放粒子:

void PopStarManager::ShowParticle( CCPoint _ptOrigin , int _iCate )
{
	CCParticleSystemQuad* particle = (CCParticleSystemQuad*)m_dataGameParticle->objectForKey(CCString::createWithFormat("pop_star%d.plist",_iCate)->getCString());
	if(particle==NULL) return;
	CCParticleSystemQuad* newParticle = CCParticleSystemQuad::create();
	newParticle->setAngle(particle->getAngle());
	newParticle->setAngleVar(particle->getAngleVar());
	newParticle->setBlendFunc(particle->getBlendFunc());//必须放setBlendAdditive上面
	newParticle->setBlendAdditive(particle->isBlendAdditive());
	
	newParticle->setDuration(particle->getDuration());
	newParticle->setTotalParticles(newParticle->getTotalParticles());

	newParticle->setContentSize(particle->getContentSize());
	newParticle->setEndColor(particle->getEndColor());
	newParticle->setEndColorVar(particle->getEndColorVar());
	newParticle->setStartColor(particle->getStartColor());
	newParticle->setStartColorVar(particle->getStartColorVar());
	newParticle->setStartSize(particle->getStartSize());
	newParticle->setStartSizeVar(particle->getStartSizeVar());
	newParticle->setEndSize(particle->getEndSize());
	newParticle->setEndSizeVar(particle->getEndSizeVar());
	newParticle->setPosition(particle->getPosition());
	newParticle->setPosVar(particle->getPosVar());
	newParticle->setStartSpin(particle->getStartSpin());
	newParticle->setStartSpinVar(particle->getStartSpinVar());
	newParticle->setEndSpin(particle->getEndSpin());
	newParticle->setEndSpinVar(particle->getEndSpinVar());

	newParticle->setEmitterMode(particle->getEmitterMode());

	if( newParticle->getEmitterMode() == kCCParticleModeGravity ) 
	{
		// gravity
		newParticle->setGravity(particle->getGravity());

		// speed
		newParticle->setSpeed(particle->getSpeed());
		newParticle->setSpeedVar(particle->getSpeedVar());

		// radial acceleration
		newParticle->setRadialAccel(particle->getRadialAccel());
		newParticle->setRadialAccelVar(particle->getRadialAccelVar());


		// tangential acceleration
		newParticle->setTangentialAccel(particle->getTangentialAccel());
		newParticle->setTangentialAccelVar(particle->getTangentialAccelVar());
		// rotation is dir
		newParticle->setRotationIsDir(particle->getRotationIsDir());
	}

	// or Mode B: radius movement
	else if(  newParticle->getEmitterMode()  == kCCParticleModeRadius) 
	{
		newParticle->setStartRadius(particle->getStartRadius());
		newParticle->setStartRadiusVar(particle->getStartRadiusVar());
		newParticle->setEndRadius(particle->getEndRadius());
		newParticle->setEndRadiusVar(particle->getEndRadiusVar());
		newParticle->setRotatePerSecond(particle->getRotatePerSecond());
		newParticle->setRotatePerSecondVar(particle->getRotatePerSecondVar());
	}
	newParticle->setLife(particle->getLife());
	newParticle->setLifeVar(particle->getLifeVar());
	newParticle->setEmissionRate(particle->getEmissionRate());
	newParticle->setTexture(particle->getTexture());
	newParticle->setAutoRemoveOnFinish(true);

	
	newParticle->setPosition(_ptOrigin);
	m_ParticleBatchNodeStar->addChild(newParticle);
}


在 Unity 中判断粒子系统的播放进度,可以通过访问 `ParticleSystem` 组件的相关属性来实现。Unity 提供了多种方法来获取和控制粒子系统的状态,包括播放、暂停、停止以及查询当前的播放时间。 ### 获取粒子系统的播放进度 1. **使用 `time` 属性** `ParticleSystem` 组件有一个 `time` 属性,表示粒子系统已经播放的时间(以秒为单位)。通过比较 `time` 和粒子系统的总持续时间(`duration`),可以计算出当前的播放进度百分比。 示例代码如下: ```csharp using UnityEngine; public class ParticleSystemProgress : MonoBehaviour { public ParticleSystem particleSystem; void Update() { if (particleSystem != null) { float progress = particleSystem.time / particleSystem.main.duration; Debug.Log("Particle System Progress: " + progress * 100 + "%"); } } } ``` 在这个示例中,`particleSystem.time` 表示粒子系统已经运行的时间,而 `particleSystem.main.duration` 表示整个粒子系统的持续时间。通过将两者相除,可以得到当前的播放进度百分比。 2. **监听粒子系统的状态** 如果需要在粒子系统播放到某个特定时间点时执行某些操作,可以结合 `Update` 方法和 `time` 属性来进行判断。例如,当 `time` 达到某个阈值时触发特定的逻辑。 ```csharp void Update() { if (particleSystem != null) { if (particleSystem.time >= 2.0f) // 当播放到 2 秒时 { Debug.Log("Reached 2 seconds mark"); // 执行其他逻辑 } } } ``` 3. **使用 `isPlaying` 属性** `ParticleSystem` 还提供了 `isPlaying` 属性,用于判断粒子系统是否正在播放。如果粒子系统处于播放状态,则返回 `true`,否则返回 `false`。 ```csharp if (particleSystem.isPlaying) { Debug.Log("Particle System is playing"); } else { Debug.Log("Particle System is not playing"); } ``` 4. **使用 `isPaused` 属性** `isPaused` 属性用于判断粒子系统是否被暂停。如果粒子系统被暂停,则返回 `true`,否则返回 `false`。 ```csharp if (particleSystem.isPaused) { Debug.Log("Particle System is paused"); } else { Debug.Log("Particle System is not paused"); } ``` 5. **使用 `isStopped` 属性** `isStopped` 属性用于判断粒子系统是否已经停止。如果粒子系统已经停止,则返回 `true`,否则返回 `false`。 ```csharp if (particleSystem.isStopped) { Debug.Log("Particle System is stopped"); } else { Debug.Log("Particle System is not stopped"); } ``` ### 控制粒子系统的播放 除了获取播放进度外,Unity 还提供了多种方法来控制粒子系统的播放行为: - **播放粒子系统**:使用 `Play()` 方法启动粒子系统。 ```csharp particleSystem.Play(); ``` - **暂停粒子系统**:使用 `Pause()` 方法暂停粒子系统的播放。 ```csharp particleSystem.Pause(); ``` - **停止粒子系统**:使用 `Stop()` 方法停止粒子系统的播放。 ```csharp particleSystem.Stop(); ``` - **重置粒子系统的播放时间**:使用 `Clear()` 方法清除粒子系统的状态,并将播放时间重置为 0。 ```csharp particleSystem.Clear(); ``` ### 总结 通过上述方法,开发者可以轻松地获取和控制 Unity 中粒子系统的播放进度。这些功能不仅可以帮助开发者更好地管理粒子效果的播放,还可以用于实现更复杂的交互逻辑,例如在粒子系统播放到某个时间点时触发特定事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值