回顾一下上一次的教程orz,也许有许多地方没有说清楚的,我补充一下
首先是OT的预设物体有:
是不是看到了熟悉的Sprite等物体,首先OT这个物体是一定要一开始就拖到场景中了,它作为一个管理全局的OT物体而存在。
然后是Sprites,Sprites简单来说只有3种:不会自己主动动画的Sprite,会自己主动动画的AnimatingSprite,能填充一定区域范围的FilledSprite,这三种Sprite是直接放进场景中的,
Sprites目录下的Animation是根据SpriteAtlas或者是SpriteSheet的Texture来生成序列帧动画的,主要是设置从第几帧到第几帧是什么动画,这样子方便AnimatingSprite的调用,这些Animation就是放到场景中的OT下的Animation目录中的
Sprites目录下的SpriteAtlas和SpriteSheet都是用来管理Texture的,以后说。
话说,unity有一点我超不喜欢的就是:一个在场景中的物体带有collior 可我不知道是自己附上去的还是通过代码添加的orz
下面是附C#代码
- public class CExample2 : MonoBehaviour {
- /// <exclude />
- public OTSprite blockPrototype; // prototype to create blocks
- /// <exclude />
- public OTSprite starPrototype; // prototype to create stars
- bool initialized = false; // initialization indicator
- // Create the objects of this demo using provided prototypes
- void CreateObjects()
- {
- // Find the empty that will act as the block container
- GameObject blocks = GameObject.Find("Blocks");
- if (blocks!=null)
- {
- // Calculate the horizontal number of blocks for the current resolution
- int c = (Screen.width - 20) / 85;
- // Calculate horizontal center spacing
- int s = (Screen.width - (c * 85)) / 2;
- // Create bottom horizontal blocks
- for (int x = 0; x < c; x++)
- {
- // Create a new block
- OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
- // Set block's position
- b.position = new Vector2(s + 50 + (x * 85)- (Screen.width/2), 20 - (Screen.height/2));
- // Set block's name
- b.name = "bottom" + x;
- // Link to parent
- b.transform.parent = blocks.transform;
- }
- // Create top horizontal blocks
- for (int x = 0; x < c; x++)
- {
- // Create a new block
- OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
- // Set block's position
- b.position = new Vector2(s + 50 + (x * 85) - (Screen.width / 2), Screen.height - 20 - (Screen.height / 2));
- // Set block's name
- b.name = "top" + x;
- // Link to parent
- b.transform.parent = blocks.transform;
- }
- // Calculate the vertical number of blocks for the current resolution
- c = (Screen.height - 50) / 85;
- // Calculate vertical center spacing
- s = (Screen.height - (c * 85)) / 2;
- // Create left vertical blocks
- for (int x = 0; x < c; x++)
- {
- // Create a new block
- OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
- // Rotate this block 90 degrees
- b.rotation = 90;
- // Set block's position
- b.position = new Vector2(20 - (Screen.width / 2), (Screen.height/2) - 40 - s - (x*85) );
- // Set block's name
- b.name = "left" + x;
- // Link block to parent
- b.transform.parent = blocks.transform;
- }
- // Create right vertical blocks
- for (int x = 0; x < c; x++)
- {
- // Create new block
- OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
- // Rotate block 90 degrees
- b.rotation = 90;
- // Set block's position
- b.position = new Vector2((Screen.width / 2)-20, (Screen.height / 2) - 40 - s - (x * 85));
- // Set block's name
- b.name = "right" + x;
- // Link block to parent
- b.transform.parent = blocks.transform;
- }
- }
- // Find the empty that will act as the stars container
- GameObject stars = GameObject.Find("Stars");
- if (stars != null)
- {
- // We will create 50 stars
- int c = 50;
- for (int x = 0; x < c; x++)
- {
- // Create a new star
- OTSprite s = (Instantiate(starPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
- // Set star's random position
- s.position =
- new Vector2(
- -1 * (Screen.width / 2) + 50 + Random.value * (Screen.width - 100),
- (Screen.height / 2) - 40 - Random.value * (Screen.height - 80));
- // Set star's name
- s.name = "star" + x;
- s.depth = 100 + x;
- // Link star to parent
- s.transform.parent = stars.transform;
- }
- }
- }
- // Application initialization
- void Initialize()
- {
- // Create all objects for this demo
- CreateObjects();
- // Set initialization notifier - we only need to initialize once.
- initialized = true;
- }
- // Update is called once per frame
- void Update () {
- // Only go one when Orthello is initialized
- if (!OT.isValid) return;
- // Call initialization once from this Update() so we can be sure all
- // Orthello objects have been started
- if (!initialized)
- Initialize();
- }
- }
下面是运动的物体,简称star
- public class CStar2 : MonoBehaviour {
- OTSprite sprite; // This star's sprite class
- Vector2 speed = // Star movement speed / second
- new Vector2(150, 150);
- float rotation = 90; // Star rotation speed / second
- Color startColor =
- new Color(0.5f, 1f, 0.5f); // Star's tint color
- Color stayColor =
- new Color(1f, 1f, 1f); // Star's tint color when overlapping
- // Use this for initialization
- void Start () {
- // get this star's sprite class
- sprite = GetComponent<OTSprite>();
- // Set this sprite's stay/exit/collision delegates
- // HINT : We could use sprite.InitCallBacks(this) as well.
- // but because delegates are the C# way we will use this technique
- sprite.onStay = OnStay;
- sprite.onExit = OnExit;
- sprite.onCollision = OnCollision;
- // Create a random speed for this star
- speed = new Vector2(150 + 150 * Random.value, 150 + 150 * Random.value);
- // Set star's color
- sprite.tintColor = startColor;
- // register the start material so we can use it later for assignment
- OT.RegisterMaterial("Star-start", new Material(sprite.material));
- var m = new Material(sprite.material);
- // register the material so we can use it later for assignment
- m.SetColor("_EmisColor", stayColor);
- OT.RegisterMaterial("Star-stay", m);
- }
- // Update is called once per frame
- void Update () {
- // adjust this star's position
- sprite.position += speed * Time.deltaTime;
- // adjust this star's rotation
- sprite.rotation += (rotation * Time.deltaTime);
- }
- // OnStay delegate is called when star enters (overlaps) another 'collidable' object
- // !IMPORTANT - This sprite's collidable setting has to be true otherwide
- // collision delegates will not be called
- /// <exclude />
- public void OnStay(OTObject owner)
- {
- // check if we entered another star and adjust color if we did
- if (owner.collisionObject.name.IndexOf("star") == 0)
- sprite.material = OT.LookupMaterial("Star-stay");
- }
- // OnExit delegate is called when star no longer overlaps another 'collidable' object
- /// <exclude />
- public void OnExit(OTObject owner)
- {
- // check if we have left another star and adjust color if we did
- if (owner.collisionObject.name.IndexOf("star") == 0)
- sprite.material = OT.LookupMaterial("Star-start");
- }
- // OnCollision delegate is called when star collides with another 'collidable' object
- // HINT - OnEnter and OnCollision delegates are called exactly at the same time, the only
- // difference is their naming convention
- /// <exclude />
- public void OnCollision(OTObject owner)
- {
- // check if we collided with a top block and adjust our speed and rotation accordingly
- if (owner.collisionObject.name.IndexOf("top") == 0 && speed.y > 0)
- {
- speed = new Vector2(speed.x, speed.y * -1);
- if ((speed.x < 0 && rotation > 0) || (speed.x > 0 && rotation < 0))
- rotation *= -1;
- }
- else
- // check if we collided with a bottom block and adjust our speed and rotation accordingly
- if (owner.collisionObject.name.IndexOf("bottom") == 0 && speed.y < 0)
- {
- speed = new Vector2(speed.x, speed.y * -1);
- if ((speed.x < 0 && rotation < 0) || (speed.x > 0 && rotation > 0))
- rotation *= -1;
- }
- else
- // check if we collided with a left block and adjust our speed and rotation accordingly
- if (owner.collisionObject.name.IndexOf("left") == 0 && speed.x < 0)
- {
- speed = new Vector2(speed.x * -1, speed.y);
- if ((speed.y < 0 && rotation > 0) || (speed.y > 0 && rotation < 0))
- rotation *= -1;
- }
- else
- // check if we collided with a right block and adjust our speed and rotation accordingly
- if (owner.collisionObject.name.IndexOf("right") == 0 && speed.x > 0)
- {
- speed = new Vector2(speed.x * -1, speed.y);
- if ((speed.y < 0 && rotation < 0) || (speed.y > 0 && rotation > 0))
- rotation *= -1;
- }
- }
- }
下面是边界
- public class CBlock2 : MonoBehaviour {
- OTSprite sprite; // This block's sprite class
- bool colorFade = false; // color fade notifier
- float fadeTime = 0; // fade time counter
- float fadeSpeed = 0.25f; // fade speed
- Color startColor =
- new Color(0.3f, 0.3f, 0.3f); // block color
- // Use this for initialization
- void Start () {
- // Lookup this block's sprite
- sprite = GetComponent<OTSprite>();
- // Set this sprite's collision delegate
- // HINT : We could use sprite.InitCallBacks(this) as well.
- // but because delegates are the C# way we will use this technique
- sprite.onCollision = OnCollision;
- // Set this block's tinting to the start color
- sprite.tintColor = startColor;
- // Register this material with Orthello so we can re-use it later
- OT.RegisterMaterial("Block-start", new Material(sprite.material));
- // Generate Highlight materials and store them
- for (int i=0; i<10; i++)
- {
- var m = new Material(sprite.material);
- m.SetColor("_EmisColor", Color.Lerp(Color.white,startColor,0.1f * i));
- OT.RegisterMaterial("Block-tint"+i, m);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (colorFade)
- {
- // We are color fading so set block's color to fade time dependend color
- int fadeIndex = (int)(Mathf.Floor((fadeTime / fadeSpeed) * 10));
- sprite.material = OT.LookupMaterial("Block-tint" + fadeIndex);
- // Incement fade time
- fadeTime += Time.deltaTime;
- if (fadeTime >= fadeSpeed)
- {
- // We have faded long enough
- colorFade = false;
- if (OT.LookupMaterial("Block-start") == null)
- print("Block-start material not found!");
- // Set block's color to start color
- sprite.material = OT.LookupMaterial("Block-start");
- }
- }
- }
- // This method will be called when this block is hit.
- /// <exclude />
- public void OnCollision(OTObject owner)
- {
- // Set color fading indicator
- colorFade = true;
- // Reset fade time
- fadeTime = 0;
- }
- }
先贴完代码,以后慢慢补全