我希望蓝色充满。
但它只会扩散到一半。
https://i.stack.imgur.com/UQsiB.png
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
Direction currentDir;
Vector2 input;
bool isMoving = false;
Vector3 startPos;
Vector3 endPos;
float t;
public Sprite northSprite;
public Sprite eastSprite;
public Sprite southSprite;
public Sprite westSprite;
public float walkSpeed = 1f;
public bool isAllowedToMove = true;
void Start()
{
isAllowedToMove = true;
}
void Update () {
if(!isMoving && isAllowedToMove)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
input.y = 0;
else
input.x = 0;
if(input != Vector2.zero)
{
if(input.x < 0)
{
currentDir = Direction.West;
}
if(input.x > 0)
{
currentDir = Direction.East;
}
if(input.y < 0)
{
currentDir = Direction.South;
}
if (input.y > 0)
{
currentDir = Direction.North;
}
switch(currentDir)
{
case Direction.North:
gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
break;
case Direction.East:
gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
break;
case Direction.South:
gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
break;
case Direction.West:
gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
break;
}
StartCoroutine(Move(transform));
}
}
}
public IEnumerator Move(Transform entity)
{
isMoving = true;
startPos = entity.position;
t = 0;
endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + System.Math.Sign(input.y), startPos.z);
while (t < 1f)
{
t += Time.deltaTime * walkSpeed;
entity.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
isMoving = false;
yield return 0;
}
}
enum Direction
{
North,
East,
South,
West
}
反向操作也会失败。
https://i.stack.imgur.com/rf00I.png
在两种情况下都很好。
https://i.stack.imgur.com/21ORb.png
答案 0 :(得分:0)
用Expanded
将要填充空格的那一个包裹起来。如果您希望红色和蓝色都填充行,请用Expanded
包裹它们,并使用flex属性为其赋予自己的比例
class TestScene extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Row(
children: <Widget>[
Container(
color: Colors.red,
child: const Text('1'),
),
Expanded(
child: Container(
color: Colors.blue,
child: const Text('1234567890123456789012345678901234567890'),
),
),
],
),
);
}
}
答案 1 :(得分:0)
这是一种方法! (要占用剩余空间)
String one='1',two='1234567890123456789012345678901234567890';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
backgroundColor:Colors.white,
body: Row(
mainAxisSize:MainAxisSize.max,
children: <Widget>[
one.length>=two.length ?
Expanded(
child: Container(
color: Colors.red,
child: Text(one),
),
):Container(
color: Colors.red,
child: Text(one),
),
two.length>=one.length ?
Expanded(
child: Container(
color: Colors.blue,
child: Text(two),
),
):Container(
color: Colors.blue,
child: Text(two),
),
],
),
);
}
或者要保持颜色大小相等,只需在两个小部件中使用Expanded