Sprite
How do I use Sprite
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: MoveByKeys.cs
Copy
26 public void Start()
27 {
28 //enabled = photonView.isMine;
29 this.isSprite = (GetComponent
30
31 this.body2d = GetComponent
32 this.body = GetComponent
33 }
File name: MoveByKeys.cs
Copy
37 public void FixedUpdate()
38 {
39 if (!photonView.isMine)
40 {
41 return;
42 }
43
44 if (Input.GetKey(KeyCode.A))
45 {
46 transform.position += Vector3.left*(this.Speed*Time.deltaTime);
47 }
48
49 if (Input.GetKey(KeyCode.D))
50 {
51 transform.position += Vector3.right*(this.Speed*Time.deltaTime);
52 }
53
54 // jumping has a simple "cooldown" time but you could also jump in the air
55 if (this.jumpingTime <= 0.0f)
56 {
57 if (this.body != null || this.body2d != null)
58 {
59 // obj has a Rigidbody and can jump (AddForce)
60 if (Input.GetKey(KeyCode.Space))
61 {
62 this.jumpingTime = this.JumpTimeout;
63
64 Vector2 jump = Vector2.up*this.JumpForce;
65 if (this.body2d != null)
66 {
67 this.body2d.AddForce(jump);
68 }
69 else if (this.body != null)
70 {
71 this.body.AddForce(jump);
72 }
73 }
74 }
75 }
76 else
77 {
78 this.jumpingTime -= Time.deltaTime;
79 }
80
81 // 2d objects can't be moved in 3d "forward"
82 if (!this.isSprite)
83 {
84 if (Input.GetKey(KeyCode.W))
85 {
86 transform.position += Vector3.forward*(this.Speed*Time.deltaTime);
87 }
88
89 if (Input.GetKey(KeyCode.S))
90 {
91 transform.position -= Vector3.forward*(this.Speed*Time.deltaTime);
92 }
93 }
94 }
File name: Cell.cs
Copy
34 public void Set(Seed seed)
35 {
36 Content = seed;
37
38 switch (seed)
39 {
40 case Seed.Empty:
41 spriteCross.SetActive(false);
42 spriteNought.SetActive(false);
43 break;
44 case Seed.Cross:
45 spriteCross.SetActive(true);
46 spriteNought.SetActive(false);
47 break;
48 case Seed.Nought:
49 spriteCross.SetActive(false);
50 spriteNought.SetActive(true);
51 break;
52 }
53 }
File name: PlayerCtrl.cs
Copy
30 void Start () {
31 rb = GetComponent
32 sr = GetComponent
33 anim = GetComponent
34 }
File name: UIgame.cs
Copy
59 public void ShowLives(int lives)
60 {
61 if (lives <= 0)
62 {
63 lifeBar.enabled = false;
64 FailScreen.SetActive(true);
65 return;
66 }
67 else
68 lifeBar.sprite = hearts[lives - 1];
69 }
File name: Structure.cs
Copy
46 void OnCollisionEnter2D(Collision2D collision){
47 if(collision.relativeVelocity.magnitude > damageCounter){
48 hitpoints -= Mathf.RoundToInt (collision.relativeVelocity.magnitude);
49 UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
50 }
51
52
53 if (hitpoints <= 50) {
54 spriteRenderer.sprite = sprite [0];
55 if(counter == 2){
56 AudioManager ();
57 counter--;
58 }
59
60 }
61
62 if(hitpoints <= 30){
63 spriteRenderer.sprite = sprite [1];
64 if(counter == 1){
65 AudioManager ();
66 counter--;
67 }
68 }
69
70
71 if(hitpoints <= 0){
72 Destroyed ();
73
74 if(collision.gameObject.CompareTag("Player Bullet")){
75 bounce = collision.transform.GetComponent
76 bounce.y = 0f;
77 collision.transform.GetComponent
78 }
79 }
80 }
File name: DialogUnity.cs
Copy
26 public void setText(string oneline)
27 {
28 bitmapFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", label1);
29 bitmapFont.setText(oneline, 0, 15);
30 Transform[] fontTransforms = label1.GetComponentsInChildren
31 for (int i = 0; i < fontTransforms.Length; i++)
32 {
33 if (fontTransforms[i].gameObject.GetComponent
34 {
35 fontTransforms[i].gameObject.layer = LayerMask.NameToLayer("GUI");
36 fontTransforms[i].gameObject.GetComponent
37 }
38 }
39 }
File name: DialogUnity.cs
Copy
41 public void setText(string line1, string line2)
42 {
43 bitmapFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", label1);
44 bitmapFont.setText(line1, 0, 15);
45 Transform[] fontTransforms = label1.GetComponentsInChildren
46 for (int i = 0; i < fontTransforms.Length; i++)
47 {
48
49 if (fontTransforms[i].gameObject.GetComponent
50 {
51 fontTransforms[i].gameObject.layer = LayerMask.NameToLayer("GUI");
52 fontTransforms[i].gameObject.GetComponent
53 }
54 }
55
56 BitmapFont bitmapFont2 = new BitmapFont(bitmapFont, label2);
57 bitmapFont2.setText(line2, 0, 15);
58 Transform[] fontTransforms2 = label2.GetComponentsInChildren
59 for (int i = 0; i < fontTransforms2.Length; i++)
60 {
61 if (fontTransforms2[i].gameObject.GetComponent
62 {
63 fontTransforms2[i].gameObject.layer = LayerMask.NameToLayer("GUI");
64 fontTransforms2[i].gameObject.GetComponent
65 }
66 }
67 }
File name: NextGuideClickListener.cs
Copy
22 public override void OnTouchDown()
23 {
24 if (InputController.Name != InputNames.DIALOG) return;
25 base.OnTouchDown();
26 gameObject.GetComponent
27 SoundManager.playButtonSound();
28 }
Download file with original file name:Sprite
Sprite 126 lượt xem
Gõ tìm kiếm nhanh...