惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
P
Privacy International News Feed
T
Tenable Blog
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
月光博客
月光博客
T
The Blog of Author Tim Ferriss
Scott Helme
Scott Helme
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
C
Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Security Latest
Security Latest
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
博客园 - 三生石上(FineUI控件)
I
InfoQ
Spread Privacy
Spread Privacy
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
A
About on SuperTechFans
博客园_首页
Engineering at Meta
Engineering at Meta
Project Zero
Project Zero
Latest news
Latest news

博客园 - 华安

Unity中 onCollisionEnter2D与OnTriggerEnter2D 区别 asp.netCore中给动态请求路径加客户端缓存 netCore中获取客户端真实的IP引起的思考 Unity 相机:正交 (Orthographic) vs 透视 (Perspective) unity中的button中的onclick控制面板中四个参数的意思分别是什么 微信小程序开发中触发 onshow的几种特殊情况 SQL Server备份心得 MYSQL 备份数据库 微信与支付支付功能开发 微信小程序中页面配置下拉刷新 unity中 相机没有视锥效果线框了,如何打开 JSONPath表达式 C# 中的操作JSON类 JObject cookie中的 HttpOnly 、Secure、SameSite 解释 Spring-boot 中基于 IP 的限流和自动封禁 Filter 登录 用 HMAC-SHA256 实现 TwoFA(二重验证)的坑 利用Spring Boot的 filter 结合ConcurrentHashMap 实现“同一IP每分钟最多允许300个404请求,超出后禁用30分钟访问” pixi-filters中的BackdropBlurFilter使用注意事项 PixiJS中的 SplitBitmapText.chars中的每个 BitmapText的x值分析 legend隐藏不想显示的图例 spring-boot HttpServletResponse response.sendRedirect是会跳转到 http而不是https springboot获取post请求参数 Javascript如何判断是触摸屏还是PC端 JavaScript获取鼠标点一个元素,获取鼠标点击的元素内的位置 spring-boot中配置Mongodbd的问题小结 Rest Template中添加 PATCH请求。 CSS实现修改CheckBox样式 SpringBoot+Thyemleaf报错:Error resolving template Template might not exist or might not be accessible div display flex 如何出现横向滚动条
Unity 中区别,public 和 [SerializeField]
华安 · 2026-07-25 · via 博客园 - 华安

在 Unity 中,public[SerializeField] 都用于在 Inspector 中显示字段,但它们的核心区别在于访问权限和封装性。

核心区别

特性public[SerializeField]
Inspector 可见性 ✅ 可见 ✅ 可见
外部访问权限 ✅ 任何类都可访问 ❌ 只有本类可访问
封装性 ❌ 破坏封装 ✅ 保持封装
使用场景 需要外部访问 仅需 Inspector 编辑

详细说明

public 字段

public class Player : MonoBehaviour
{
    public float speed = 5f;  // 其他类可以直接修改
}
  • 任何其他类都可以通过 player.speed 访问和修改
  • 破坏了面向对象的封装原则
  • 可能导致不可预期的修改

[SerializeField] 私有字段

public class Player : MonoBehaviour
{
    [SerializeField] private float speed = 5f;  // 只有本类可修改
}
  • 保持私有性,其他类无法直接访问
  • 仍然在 Inspector 中可见可编辑
  • 遵循封装原则

推荐实践

推荐使用 [SerializeField] 的场景

public class Player : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private int maxHealth = 100;
    
    // 如果需要外部访问,提供公共属性
    public float MoveSpeed => moveSpeed;
    public int MaxHealth => maxHealth;
}

应该使用 public 的场景

public class GameManager : MonoBehaviour
{
    // 其他类需要频繁修改
    public static GameManager Instance;
    
    // 需要被其他系统直接调用
    public void StartGame() { }
}

进阶技巧

使用属性封装

public class Player : MonoBehaviour
{
    [SerializeField] private int health = 100;
    
    public int Health 
    { 
        get => health;
        set 
        {
            health = Mathf.Clamp(value, 0, maxHealth);
            OnHealthChanged?.Invoke(health);
        }
    }
    
    public event System.Action<int> OnHealthChanged;
}

使用 [SerializeField] 的扩展

public class Weapon : MonoBehaviour
{
    [SerializeField] private int damage = 10;
    [SerializeField] [Range(0, 100)] private int accuracy = 80;
    [SerializeField] [Tooltip("子弹速度,单位:米/秒")] private float bulletSpeed = 100f;
    [SerializeField] [HideInInspector] private int serialNumber; // 序列号不在Inspector显示
}

总结

  • 优先使用 [SerializeField] private:保持封装性,只暴露必要的 Inspector 接口
  • 仅在需要外部访问时使用 public:如静态单例、公共方法等
  • 考虑使用属性:在需要额外逻辑(验证、触发事件)时替代直接暴露字段
  • 团队协作:建议制定统一的编码规范,明确何时使用哪种方式

这样做既能保持代码的健壮性,又能充分利用 Unity Inspector 的可视化编辑优势。