loading Bar

.h文件:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
//#include "UIScene.h"
class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();
    
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
	void update(float delta)override;
protected:
	int _count;
	//cocos2d::Layer* _uiLayer;
	//cocos2d::ui::Layout* _widget;
	//cocos2d::ui::Text* _sceneTitle;
    // implement the "static create()" method manually
public:

    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

.cpp文件:

#include "HelloWorldScene.h"
#include "ui/CocosGUI.h"

USING_NS_CC;
using namespace cocos2d::ui;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
    
    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label, 1);

    // add "HelloWorld" splash screen"
   // auto Sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
	//Sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));


    // add the sprite as a child to this layer
   // this->addChild(Sprite, 0);
	scheduleUpdate();

	//Size widgetSize = _widget->getContentSize();

	// Add the alert
	Text* alert = Text::create("Test LoadingBar Change Direction",
		"fonts/Marker Felt.ttf", 30);
	alert->setColor(Color3B(159, 168, 176));
	alert->setPosition(Vec2(visibleSize.width / 2.0f,
		visibleSize.height / 2.0f - alert->getContentSize().height * 1.75f));
	addChild(alert);

	// Create the loading bar
	LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
	loadingBar->setTag(0);
	loadingBar->setPosition(Vec2(visibleSize.width / 2.0f,
		visibleSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
	//auto loadingBarCopy = (LoadingBar*)loadingBar->clone();
	//loadingBarCopy->setTag(1);
	//loadingBarCopy->setPosition(loadingBar->getPosition()
	//	+ Vec2(0, -40));
	//loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);

	Button* button = Button::create("cocosui/animationbuttonnormal.png",
		"cocosui/animationbuttonpressed.png");
	button->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 50));
	button->setTitleText("Click to change direction!");

	button->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
	{
		if (type == Widget::TouchEventType::ENDED)
		{
			if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
			{
				loadingBar->setDirection(LoadingBar::Direction::RIGHT);
				//loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
			}
			else
			{
				loadingBar->setDirection(LoadingBar::Direction::LEFT);
				//loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
			}
		}
	});
	addChild(loadingBar, 1);
	//addChild(loadingBarCopy, 2);
	addChild(button);
	
    return true;
}

void HelloWorld::update(float delta)
{
	_count++;
	if (_count > 100)
	{
		_count = 0;
	}
	LoadingBar* loadingBar = static_cast<LoadingBar*>(getChildByTag(0));
	//LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(getChildByTag(1));
	loadingBar->setPercent(_count);
	//loadingBarCopy->setPercent(_count);
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

效果:


可以通过在Unity中使用脚本来实现Loading Bar加载进度条。首先,您需要设置场景,然后设置一个脚本,例如LevelLoader.cs,并将其挂载到一个空对象上。在LevelLoader脚本中,定义一个LoadLevel方法,该方法将通过调用LoadAsynchronously协程来加载场景。在LoadAsynchronously协程中,使用AsyncOperation来异步加载场景,并在循环中更新加载进度。您可以将进度值调整为0到1之间的百分比,并将其传递给Slider组件以及显示当前进度的文本框。最后,通过激活loadingScreen对象来显示加载界面。 以下是一个示例的LevelLoader脚本代码: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class LevelLoader : MonoBehaviour { public GameObject loadingScreen; public Slider slider; public Text progresstext; public void LoadLevel(int sceneIndex) { StartCoroutine(LoadAsynchronously(sceneIndex)); } IEnumerator LoadAsynchronously(int sceneIndex) { AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex); loadingScreen.SetActive(true); while (!operation.isDone) { float progress = Mathf.Clamp01(operation.progress / .9f); slider.value = progress; progresstext.text = (progress * 100f).ToString("F0") + "%"; yield return null; } } } ``` 通过在您的项目中创建一个空对象,并将LevelLoader脚本挂载到该对象上,您就可以调用LoadLevel方法来加载场景,并显示Loading Bar加载进度条。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Unity | Loading Bar 如何制作一个进度条](https://blog.csdn.net/qq_30330655/article/details/105878846)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值