SHOW:
|
|
- or go back to the newest paste.
1 | using UnityEngine; | |
2 | using System.Collections; | |
3 | ||
4 | public class Lamp : MonoBehaviour | |
5 | { | |
6 | // set these up in the inspector | |
7 | // the spriteRenderers will be dragged in from the scene | |
8 | public SpriteRenderer lampSpriteRenderer, skySpriteRenderer; | |
9 | // these sprites are Assets, they should be dragged in from the project folder | |
10 | public Sprite lampOn, lampOff; | |
11 | public Sprite skyOn, skyOff; | |
12 | ||
13 | // have to add a boxcollider (or spherecollider) to this to make it actually clickable. set it to be a trigger (isTrigger checkbox) | |
14 | void OnMouseDown() | |
15 | { | |
16 | // if the lamp spriteRenderer (thing that renders the lamp onscreen) has the lampOn sprite, then ... | |
17 | if (lampSpriteRenderer.sprite == lampOn) | |
18 | { | |
19 | // we want ot set it off | |
20 | lampSpriteRenderer.sprite = lampOff; | |
21 | // and set the sky off too! | |
22 | skySpriteRenderer.sprite = skyOff; | |
23 | } | |
24 | // if the lamp spriteRenderer (thing that renders the lamp onscreen) is NOT showing the lampOn sprite, we can assume it's showing the lampOff sprite... | |
25 | else | |
26 | { | |
27 | // so turn the lamp on | |
28 | lampSpriteRenderer.sprite = lampOn; | |
29 | // and the sky too! :) | |
30 | skySpriteRenderer.sprite = skyOn; | |
31 | } | |
32 | } | |
33 | } |