如何在Swift中创建CGSize?这是我到目前为止所尝试的(但不起作用):
var s:CGSize = {10,20}
var s:CGSize = CGMakeSize(10,20)
答案 0 :(得分:83)
你的第一次尝试不会起作用,因为C结构不存在于Swift中。你需要:
let size = CGSize(width: 20, height: 30)
或者(仅限Swift 3之前,即便如此,不是首选):
let size = CGSizeMake(20,30)
(不是MakeSize)。
答案 1 :(得分:29)
从Swift 3开始,您无法再使用CGSizeMake
Swift 3的解决方案是var size = CGSize(width: 20, height: 30)
答案 2 :(得分:1)
快捷键4
void UpdateTarget()
{
GameObject[] Humans = GameObject.FindGameObjectsWithTag(humanTag);
GameObject[] Vaccines = GameObject.FindGameObjectsWithTag(vaccineTag);
GameObject[] HV = Humans.Concat(Vaccines).ToArray();
float shortestDistance = Mathf.Infinity;
GameObject nearestHuman = null;
Vector3 currentPosition = this.transform.position;
foreach (GameObject huvacs in HV)
{
Vector3 directionToTarget = huvacs.transform.position - currentPosition;
float distanceToTarget = directionToTarget.sqrMagnitude;
if (distanceToTarget < shortestDistance)
{
shortestDistance = distanceToTarget;
nearestHuman = huvacs;
}
else
{
targetHuman = null;
}
}
if (nearestHuman != null && shortestDistance <= range)
{
targetHuman = nearestHuman.transform;
infectionAgent.SetDestination(targetHuman.transform.position);
}
}