网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
01月03日漏签0天
unity吧 关注:52,155贴子:145,752
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 2回复贴,共1页
<<返回unity吧
>0< 加载中...

震惊:U3D官方教程中竟藏着这种秘密...

  • 取消只看楼主
  • 收藏

  • 回复
  • yxshuafenhao
  • 默默无闻
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
新手标题党了一波....
下面进入正题
新手疑问:不需要引用也不需要new,即可使用对象?
在U3D的官方教程“2D UFO”小游戏中,创建了两个TEXT对象用于显示分数和胜利信息,但仅限于public Text countText;这种声明而已,连new都没用,后面就可以直接引用并修改这些组件的值来达到现实效果,这是为什么呢,为什么不需要countText=foundobject(XX)<text>.text来引用到对象,就可以直接修改值?

找遍了整个文档,都没有发现引用,它确实只是声明了一下,连new都没有就可以使用了
PS:本人新手,在研究教程中


  • yxshuafenhao
  • 默默无闻
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
贴一下完整的代码:


2026-01-03 12:07:43
广告
不感兴趣
开通SVIP免广告
  • yxshuafenhao
  • 默默无闻
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
using UnityEngine;
using System.Collections;
//Adding this allows us to access members of the UI namespace including Text.
using UnityEngine.UI;
public class CompletePlayerController : MonoBehaviour {
public float speed;//Floating point variable to store the player's movement speed.
public Text countText;//Store a reference to the UI Text component which will display the number of pickups collected.
public Text winText;//Store a reference to the UI Text component which will display the 'You win' message.
private Rigidbody2D rb2d;//Store a reference to the Rigidbody2D component required to use 2D Physics.
private int count;//Integer to store the number of pickups collected so far.
// Use this for initialization
void Start()
{
//Get and store a reference to the Rigidbody2D component so that we can access it.
rb2d = GetComponent<Rigidbody2D> ();
//Initialize count to zero.
count = 0;
//Initialze winText to a blank string since we haven't won yet at beginning.
winText.text = "";
//Call our SetCountText function which will update the text with the current value for count.
SetCountText ();
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
//Store the current horizontal input in the float moveHorizontal.
float moveHorizontal = Input.GetAxis ("Horizontal");
//Store the current vertical input in the float moveVertical.
float moveVertical = Input.GetAxis ("Vertical");
//Use the two store floats to create a new Vector2 variable movement.
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
//Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
rb2d.AddForce (movement * speed);
}
//OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
void OnTriggerEnter2D(Collider2D other)
{
//Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
if (other.gameObject.CompareTag ("PickUp"))
{
//... then set the other object we just collided with to inactive.
other.gameObject.SetActive(false);
//Add one to the current value of our count variable.
count = count + 1;
//Update the currently displayed count by calling the SetCountText function.
SetCountText ();
}
}
//This function updates the text displaying the number of objects we've collected and displays our victory message if we've collected all of them.
void SetCountText()
{
//Set the text property of our our countText object to "Count: " followed by the number stored in our count variable.
countText.text = "Count: " + count.ToString ();
//Check if we've collected all 12 pickups. If we have...
if (count >= 12)
//... then set the text property of our winText object to "You win!"
winText.text = "You win!";
}
}


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 2回复贴,共1页
<<返回unity吧
分享到:
©2026 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示